Why does my JAR file execute at CMD, but not on double-click?
I got fed up with not being able to modify file associations in Windows 7 Control Panel and edited the registry (NOTE: it's not a bad idea at all to set a restore point before proceeding if you think there's ANY chance you may screw up)(I neither set restore point nor screwed up):
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\jar_auto_file]
@=""
"EditFlags"=hex:00,00,00,00
[HKEY_CLASSES_ROOT\jar_auto_file\shell]
[HKEY_CLASSES_ROOT\jar_auto_file\shell\open]
[HKEY_CLASSES_ROOT\jar_auto_file\shell\open\command]
@="\"C:\\Program Files\\Java\\jdk1.7.0_60\\bin\\javaw.exe\" -jar \"%1\" %*"
This assumes that .jar
files have (Default)
value jar_auto_file
. If not do this:
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\.jar]
@="jar_auto_file"
p.s. One problem I found in the registry was that jarfile
was associated with Netbeans, Chrome, and java.exe as well as with Applications\java.exe
. I deleted that entire node (after exporting it, just so I could put it back if it was essential; it wasn't). Now there is NO jarfile
in my registry and all .jar files execute as before.
This manifests itself by showing more than one file type associated with .jar
files in Control Panel. You want this:
If there's more than one item shown in Recommended programs, search registry for jarfile
and (export first, then) delete that node:
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.jar
Ok, so I was stuck on this exact issue for over a week now (it has been for a side project for which I could only devote a few hours a day).
This happened on my desktop, but for some reason wouldn't happen on the laptop.
After looking around, I found this answer and I thought I'd share it for people who are like me, found nothing useful in the accepted answer here. Credits go to anonymous Stack Overflow user, whom username I've lost in all the excitement.
As mentioned by some other answer somewhere to an seeming unrelated issue, use this little program to associate your JAR files to the 64 bit version of java:
http://johann.loefflmann.net/en/software/jarfix/index.html
Save the program somewhere and run it from command line with the parameter /64: c://path//jarfix.exe /64
Nothing else worked for me, but this was like magic. :)
Prerequisite: make sure JRE(that is included in JDK) is installed.
Solution
Open regedit and navigate to
Computer\HKEY_CLASSES_ROOT\jar_auto_file\shell\open\command
Add "-jar" to value data. For example, change from
"C:\Program Files\Java\jdk-15.0.1\bin\java.exe" "%1"
to"C:\Program Files\Java\jdk-15.0.1\bin\java.exe" "-jar" "%1"
Done
If problem still occurs, make sure you configure correct main class in project property.
The JAR is executable from CMD. That means the JAR itself is formed correctly. Good.
The only reason to fail now is that double-click produces not a right command. Expected command, as you correctly said, is
java -jar Modeler.jar
But when you associate javaw.exe with a JAR extension, I suspect it executes
javaw Modeler.jar
It is easy to check: make a javajar.cmd file, containing the following
javaw -jar %*
and associate it with JAR. If you app starts OK, I'm right. Otherwise, sorry.