java can run jar from cmd but not by double clicking
I just created a jar file using jdk7. I tried running it but kept getting an error. It turned out it was pointing at a jre6 and not the jre in jdk7. So, I used the following command to change it
ftype jarfile = "C:\path to jre\bin\javaw.exe" -jar "%1" %*
After that, I tried to double click on my jar file again, but it popped up a window asking me to choose a program to open it with. When I chose the same javaw.exe file as above it runs but exits immediately without doing anything. I can run it fine in cmd by doing the java -jar file.jar command. I even tried deleting the .jar registry key, but that just got me back to the point where it asked me to choose a program to open with. I'm running windows 7 ultimate if that matters.
EDIT: In the registry there are 3 subkeys: OpenWithList, OpenWithProgids, and UserChoice. I'v开发者_JAVA技巧e changed all the defaults to the path above except UserChoice which won't let me edit it from regedit. When I delete that and try to run the jar file, it opens up the choose a program to open with window again. For some reason it ignores the other registry keys
OK, I found the answer somewhere else but ran into this issue again so came here via google.
The solution is...
- Open up regedit.exe
- Either search for "
jre6
" or follow this:
HKLM->SOFTWARE->Classes->jarfile->shell->open->command
- Change the Data field to the directory of the javaw.exe file that's in your JDK directory.
- For the love of God, don't forget to put the weird
[-jar "%1" %*]
parts on the end of it. - Enjoy outsmarting Oracle.
Hopefully this'll save some people a bit of time.
This happened to me when I installed the JRE6
plugin for Chrome, though it could happen when installing any other JRE
I guess.
I fixed it by going to regedit and searched for jre which brought up...
HKEY_CURRENT_USER/Software/Classes/Applications/javaw.exe/shell/open/command
I changed the REG_SZ
from: "C:\Program Files (x86)\Java\jre7\bin\javaw.exe" "%1"
to: "C:\Program Files (x86)\Java\jre7\bin\javaw.exe" -jar "%1" %*
When windows asks you for a program to run it against it won't insert the needed -jar
argument in the command line.
If it's asking you for the exe
in which to run it then that means somehow your JRE
isn't associated with the .jar
extension. You'll need to modify the .jar
file registry entry so that it runs c:\path_to_jre\bin\javaw -jar %1
in order for it to work.
It used to be under File Explorer > Tools > Folder Options > File Types
.
Then look for .jar in there. You should find a text field that shows the executable to run and the command line arguments. That's what you want to modify. You can also do it in the registry editor too, but I've forgotten the HKEY
variable path. I'm sure this KBA will help:
support_microsoft_kb_950505
Check if the file type jarfile
is actually registered with the *.jar extension using
assoc .jar
Seems like .jar is not linked to jarfile.
I'm not sure exactly why none of the solutions worked, but I reinstalled jre7 and it works now.
Isn't it easier to change your java version to you rnew jdk7 by changing the CLASSPATH
environment variable? See how here
精彩评论