views:

22979

answers:

9

I have a "helloworld.jar" file. For running a JAR file I am using a command-line window and executing the following command:

    java -jar helloworld.jar

By using this command I can execute the JAR file. But instead of doing it in a command-line window, I want to execute the JAR file if I double click on the JAR file. I did some Google search for this. But I cannot do this.

For this do I need to install any software? Can any one provide me help on this?

+2  A: 

You want to check a couple of things; if this is your own jar file, make sure you have defined a Main-class in the manifest. Since we know you can run it from the command line, the other thing to do is create a windows shortcut, and modify the properties (you'll have to look around, I don't have a Windows machine to look at) so that the command it executes on open is the java -jar command you mentioned.

The other thing: if something isn't confused, it should work anyway; check and make sure you have java associated with the .jar extension.

Charlie Martin
+16  A: 

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

Or this:

  • 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 highlighted in blue is the important part execution string, 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

spilth
+2  A: 

Follow the steps described here : Executing a Jar on Vista with a double click

RealHowTo
+1  A: 

Besides all of the other suggestions, there is one other thing you need to consider. Is your helloworld.jar a console program? If it is, then I don't believe you'll be able to make it into a double-clickable jar file. Console programs use the regular cmd.exe shell window for their input and output. Usually the jar "launcher" is bound to javaw.exe which doesn't create a command-shell window.

+1  A: 

The following steps at rcanblog describe the easiest way to create a jar file in windows http://rcanblog.com/jar-java-archive-format-140/

+2  A: 

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)

jarfile.jar parameter

No joy, because this is being translated to:

javaw.exe -jar jarfile.jar parameter

it won't work, however:

java.exe -jar jarfile.jar parameter

does work.

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" %*

you can then type:

jarfile.jar parameter

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 common tidy solution here, but you would have the same problem with any other console application. There is always a compromise.

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.

Bob
+2  A: 

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.

Hendy Irawan
That's a great suggestion. In Windows 7 at least you can change file associations by going to the start menu and typing "change file type".
Jon
+1  A: 

In Windows XP * you need just 2 shell commands:

   C:\>ftype myjarfile="C:\JRE1.6\bin\javaw.exe" -jar "%1" %* 
   C:\>assoc .jar=myjarfile  

obviously using the correct path for the JRE and any name you want instead of myjarfile.

To just check the current settings:

   C:\>assoc .jar  
   C:\>ftype jarfile  

this time using the value returned by the first command, if any, instead of jarfile.

* not tested with Windows 7

Carlos Heuberger
A: 

if u have a jar file called Example.jar ... follow these rules: 1)Open notepad 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

Pusu