Cmd java jar file

Running JAR file on Windows

I have a JAR file named helloworld.jar. In order to run it, I’m executing the following command in a command-line window:

This works fine, but how do I execute it with double-click instead? Do I need to install any software?

25 Answers 25

Easiest route is probably upgrading or re-installing the Java Runtime Environment (JRE).

  • Open the Windows Explorer, from the Tools select ‘Folder Options. ‘
  • Click the File Types tab, scroll down and select JAR File type.
  • Press the Advanced button.
  • In the Edit File Type dialog box, select open in Actions box and click Edit.
  • Press the Browse button and navigate to the location the Java interpreter javaw.exe.
  • In the Application used to perform action field, needs to display something similar to C:\Program Files\Java\j2re1.4.2_04\bin\javaw.exe» -jar «%1» % (Note: the part starting with ‘javaw’ must be exactly like that; the other part of the path name can vary depending on which version of Java you’re using) then press the OK buttons until all the dialogs are closed.

i think the easy way is to right click any ,jar file > open with and chose «Java runtime environment». 🙂

doing so , always gives this error to me » Java platform SE binary has stopped working » . Can anyone help, what could be the issue?

@Faizan — Your issue sounds like a separate problem. I see a few SO and Google results for «Java platform SE binary has stopped working». I would check those out.

Читайте также:  Php result query error

same with Win 10. did this get moved somewhere with the same functionality? the Windows 10 system ‘default app by file type’ page seems to not let you edit the execution command to add a ‘jar parameter

In Windows Vista or Windows 7, the manual file association editor has been removed.

The easiest way is to run Jarfix, a tiny but powerful freeware tool. Just run it and your Java apps is back. double-clickable again.

it would be great if someone somewhere could detail the changes this executable makes so that someone could choose to repeat it manually without exetuting some random strangers compiled code.

If you need to distribute your .jar file and make it runnable at other people’s Windows computers, you can make a simple .bat file like this in the command prompt:

and place the .bat file in the same directory as your .jar file.

But how is the .bat executed? Would this be the same strategy to get the Java application to open up a command-line session as well?

If you have a jar file called Example.jar, follow these rules:

  1. Open a notepad.exe
  2. Write : java -jar Example.jar
  3. Save it with the extension .bat
  4. Copy it to the directory which has the .jar file
  5. Double click it to run your .jar file

Great way to run a jar on Windows! It also doesn’t hide the command line input, so I was able to see why my jar wasn’t running like I thought it should. Thank you.

An interesting side effect of this causes a problem when starting runnable jar files in the command prompt.

If you try (in a command prompt):

No joy, because this is being translated to the following (which doesn’t work):

javaw.exe -jar jarfile.jar parameter 

However, the following command does work:

java.exe -jar jarfile.jar parameter 

If you change the association in file manager as described above to:

"C:\Program Files\Java\j2re1.4.2_04\bin\java.exe" -jar "%1" %* 

in the command prompt and it will now work!

EDIT:(However you then get a black console window when you run a form based (non console) Java app, so this is not an ideal solution)

If you run these jar files by double clicking them in windows, no parameters will be passed so your Java code needs to handle the stack overflow exception and include a «press a key» function at the end or the window will just disappear.

In order to pass a parameter in windows you have to create a shortcut to the jar file, which includes the parameter in the target line (right click on the shortcut and select properties) you can not add parameters to the jar file icon itself in this way.

There isn’t a single, consistent solution here, but you would have the same problem with any other console application.

There is a windows freeware application called «bat to exe» which you can use to create an exe file from a .bat file with the apropriate command line in it. you can also embed the jar file in the exe with this application, and make it clean it up when it has finished running, so this may be a more elegant solution.

Источник

executing jar file through cmd

I have created a folder named «Thank» in c drive and in that folder, I have 2 java files namely T1 and myApp.

This creates a Jar file named myApp. I even wrote Main-Class: myApp in the manifest.mf file . When I try to run this by:

@tbsalling myApp.jar is stored in c drive. It further has 2 folders namely: META-INF & Thank. META-INF has a MANIFEST.MF file which contains: Main-Class: myApp; Manifest-Version: 1.0 Created-By: 1.7.0_21 (Oracle Corporation)

3 Answers 3

You should use jar cfm myApp.jar manifest.txt *.class to create the jar, so that the manifest file gets located correctly in the jar.

The right location of the manifest is META-INF/MANIFEST.MF .

Update

I have made your code work, basically by taking the files you had prepared; adding a package declaration to the java files and the manifest, and upper-casing the MyApp class from myApp. The files are arranged in this folder structure:

tbsmac:17162802-executing-java-file-through-cmd tbsalling$ ls -lR total 0 drwxr-xr-x 3 tbsalling staff 102 19 Jun 18:48 META-INF drwxr-xr-x 4 tbsalling staff 136 19 Jun 18:57 thank ./META-INF: total 8 -rw-r--r-- 1 tbsalling staff 46 19 Jun 18:49 MANIFEST.MF ./thank: total 16 -rw-r--r-- 1 tbsalling staff 124 19 Jun 18:49 MyApp.java -rw-r--r-- 1 tbsalling staff 98 19 Jun 18:48 T1.java 

The contents of the three files are:

package thank; class MyApp < public static void main(String args[]) < T1 t=new T1(); t.display(); >> 
Main-Class: thank.MyApp Manifest-Version: 1.0 

Then I run this series of commands:

tbsmac:17162802-executing-java-file-through-cmd tbsalling$ javac thank/T1.java thank/MyApp.java tbsmac:17162802-executing-java-file-through-cmd tbsalling$ jar cfm myApp.jar META-INF/MANIFEST.MF thank/*.class tbsmac:17162802-executing-java-file-through-cmd tbsalling$ java -jar myApp.jar Hey I am working 

Источник

Run jar file in command prompt [duplicate]

If you dont have an entry point defined in your manifest invoking java -jar foo.jar will not work.

Use this command if you dont have a manifest or to run a different main class than the one specified in the manifest:

java -cp foo.jar full.package.name.ClassName 

See also instructions on how to create a manifest with an entry point: https://docs.oracle.com/javase/tutorial/deployment/jar/appman.html

For Developers, I think this is a better answer than Bala R’s, although they should ideally be combined

@MoxGeek you need a static method named main . I don’t think there is a way arround this. You can still write your own Main class and import your other class from there. Extra parameters to pass to the main method just comes after your full.package.name.ClassName . You could also have a look to java REPL, like jshell and import what you need, this solution would feel more «scripting» style.

java [any other JVM options you need to give it] -jar foo.jar

You can run a JAR file from the command line like this:

Linked

Hot Network Questions

Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.7.25.43544

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Источник

Extracting .jar file with command line

To extract the files from a jar file, use x , as in:

To extract only certain files from a jar file, supply their filenames:

C:\Java> jar xf myFile.jar foo bar 

The folder where jar is probably isn’t C:\Java for you, on my Windows partition it’s:

C:\Program Files (x86)\Java\jdk[some_version_here]\bin 

Unless the location of jar is in your path environment variable, you’ll have to specify the full path/run the program from inside the folder.

EDIT: Here’s another article, specifically focussed on extracting JARs: http://docs.oracle.com/javase/tutorial/deployment/jar/unpack.html

Ok, When I do that jar xf. command, I get the error: ‘jar’ is not recognized as an internal or external command,operable program or batch file. How do I fix this?

java.io.IOException: META-INF : could not create directory at sun.tools.jar.Main.extractFile(Main.java:928) at sun.tools.jar.Main.extract(Main.java:852) at sun.tools.jar.Main.run(Main.java:242) at sun.tools.jar.Main.main(Main.java:1149) how to remove this error?

@AnilPatel I fixed this by running the command prompt as administrator (right click «run as administrator»)

here’s a stupid question — if I do this right (fixed the error messages I received first time), where are these files going to be extracted to? I’ve checked the jdk folder and the folder of the jar file to extract but I don’t see any new files

Note that a jar file is a Zip file, and any Zip tool (such as 7-Zip) can look inside the jar.

unzip file.jar -d dir_name_where_extracting

You can use the following command: jar xf rt.jar

Where X stands for extraction and the f would be any options that indicate that the JAR file from which files are to be extracted is specified on the command line, rather than through stdin.

Java has a class specifically for zip files and one even more specifically for Jar Files.

java.util.jar.JarOutputStream java.util.jar.JarInputStream 

using those you could, on a command from the console, using a scanner set to system.in

Scanner console = new Scanner(System.in); String input = console.nextLine(); 

then get all the components and write them as a file.

JarEntry JE = null; while((JE = getNextJarEntry()) != null) < //do stuff with JE >

You can also use java.util.zip.ZipInputStream instead, as seeing a JAR file is in the same format as a ZIP file, ZipInputStream will be able to handle the Jar file, in fact JarInputStream actually extends ZipInputStream.

an alternative is also instead of getNextJarEntry, to use getNextEntry

Источник

Оцените статью