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:
java -jar helloworld.jar
This works fine, but how do I execute it with double-click instead? Do I need to install any software?
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.
Which was stolen from here: http://windowstipoftheday.blogspot.com/2005/10/setting-jar-file-association.html
Read more... Read less...
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:
java -jar MyJavaTool.jar
and place the .bat file in the same directory as your .jar file.
If you have a jar file called Example.jar, follow these rules:
- Open a
- Write :
java -jar Example.jar
- Save it with the extension
- Copy it to the directory which has the
- Double click it to run your
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" %*
Then you can type:
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.
First set path on cmd(command prompt):
set path="C:\Program Files\Java\jre6\bin"
java -jar yourProgramname.jar