I made a very simple program with Eclipse Indigo and exported it as an executable jar to my desktop. If I use the command "java -jar SayHello.jar", the program runs, but if I double click SayHello.jar on my desktop, my mouse gets the spinning wheel, looking as if it is loading something, but then the wheel goes away and nothing ever happens. This happens for every jar file, not just this one. I have searched for hours on how to fix this, but nothing has worked. Any help would be greatly appreciated. Thanks!
-
2`.jar` files are not executables, they are closer in relation to `.zip` files. You need to wrap it in an executable or write a batch file to execute `java -jar SayHello.jar` when run. – Jonny Henly May 30 '15 at 00:25
-
1this is an OS problem... file extension association – ZhongYu May 30 '15 at 00:28
-
if you need to decompile the jar file, take a look at this http://jd.benow.ca/ – jinggoy May 30 '15 at 00:34
-
Thank you for your answers. How can i wrap the .jar in an executable or write a batch file to execute the command when run? And which one would be more convenient for someone if I were to send this file to someone? – Michael Miller May 30 '15 at 00:47
-
http://stackoverflow.com/questions/2622062/run-jar-from-batch-file – RisingSun May 30 '15 at 00:51
-
Ok, I figured out how to make a batch file to run it, but when it runs, the command line pops up for a second, displays "Hello World" then immediately disappears. Is there a way to make it stay up for longer or should I wrap it in an executable? If I should wrap it, how do I? Thank you for all your answers! – Michael Miller May 30 '15 at 01:10
-
All of the above comments are wrong (except possibly bayou.io's comment). You are right to expect that double-clicking a .jar file will run it in exactly the same fashion as **java -jar** *filename*, at least in Windows. You don't need to turn it into an .exe, and you don't need a batch file. My guess is that the .jar extension is associated with a version of Java older than the version you used to compile your classes. – VGR May 30 '15 at 01:30
-
Thanks! I looked up how to associate the .jar with java.exe and now double clicking works! However, when I double click to open the file, a command line pops up for a split second saying that it could not find the main class. How do I fix that? And is there a way to make the command line not disappear after a split second? Again, thank you all for your answers. – Michael Miller May 30 '15 at 04:33
-
@MichaelMiller, **You can try this** this might help, go to control panel and change the default program that is executing the `.jar` files in your computer. `Control Panel` > `Programs` > `Default Programs` > `Set Associations` > Single click over `.jar` > `Change Program` > locate the `JRE` installation folder in your PC, go to `bin` and select **`javaw.exe`** > `OK`. Try and let me know if it works. – mustangDC May 30 '15 at 04:43
-
Did you develop your program using an `IDE`? – mustangDC May 30 '15 at 04:45
-
That didn't work. I had changed it to java.exe using the same steps, which is what allowed me to double click but get the error message. Changing it to javaw.exe made it go back to how it was before, having the loading circle pop up then disappear. Strangely though, no matter what I have done, I can still run the program using the batch file and the command line. – Michael Miller May 30 '15 at 04:50
-
That's is quiet strange also, but good to hear that your problem is solved. But what about the main class, did it also vanish? – mustangDC May 30 '15 at 04:53
-
The main class problem is still there. When I double click it the command line comes up for a split second saying "Could not find or load main class" then it has the path to the file on my desktop. I think it may be a problem with my Manifest.mf? This is what's in it: Manifest-Version: 1.0 Class-Path: . Main-Class: SayHello With a blank space at the end. – Michael Miller May 30 '15 at 17:07
1 Answers
For Java on Windows (which you didn't actually specify) the default action when doubleclicking a .jar
file, as you found, is to run it with the javaw
JVM which is "GUI" mode and anything the code writes to System.out
is discarded. The java
JVM does output to the console window, but if that window was created by double-clicking it vanishes as soon as the code exits, as you found.
Note the "association" for .jar
(in HKCR\jarfile\shell\open\command
) must be
"(YOUR_JREDIR_usually_progfiles-arch-jre-version)\bin\javaw.exe" -jar "%1" %*
or java.exe
for console, in either case with the -jar
. I don't believe control panel will set extra flag arguments like that correctly, and that would account for the "cannot find main class".
A batch file with two lines java -jar myjar
and then pause
when doubleclicked should run the class and when it exits wait until you press any key before destroying the window. You can instead use timeout numseconds
to limit the wait.
ADDED: Double Clicking JAR file does not open Command Prompt has another way to keep the console window from disappearing: use cmd /s /k
instead of a batch file.

- 1
- 1

- 34,712
- 6
- 50
- 70