Michael Miller
Michael Miller

Reputation: 21

Can't open .jar using double-click, only with cmd

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!

Upvotes: 2

Views: 2020

Answers (1)

dave_thompson_085
dave_thompson_085

Reputation: 38771

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.

Upvotes: 1

Related Questions