windows java可以从cmd运行jar,但不能通过双击
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7325676/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java can run jar from cmd but not by double clicking
提问by jhlu87
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
我刚刚使用 jdk7 创建了一个 jar 文件。我尝试运行它,但一直出现错误。原来它指向的是 jre6 而不是 jdk7 中的 jre。所以,我用下面的命令来改变它
ftype jarfile = "C:\path to jre\bin\javaw.exe" -jar "%1" %*
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.
之后,我尝试再次双击我的 jar 文件,但它弹出一个窗口,要求我选择一个程序来打开它。当我选择与上面相同的 javaw.exe 文件时,它会运行但立即退出而不做任何事情。我可以通过执行 java -jar file.jar 命令在 cmd 中正常运行它。我什至尝试删除 .jar 注册表项,但这让我回到了它要求我选择要打开的程序的地步。如果这很重要,我正在运行 Windows 7 Ultimate。
EDIT: In the registry there are 3 subkeys: OpenWithList, OpenWithProgids, and UserChoice. I've 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
编辑:在注册表中有 3 个子项:OpenWithList、OpenWithProgids 和 UserChoice。除了 UserChoice 之外,我已将所有默认值更改为上面的路径,这不会让我从 regedit 编辑它。当我删除它并尝试运行 jar 文件时,它会再次打开选择要打开的程序窗口。出于某种原因,它忽略了其他注册表项
采纳答案by jhlu87
I'm not sure exactly why none of the solutions worked, but I reinstalled jre7 and it works now.
我不确定为什么所有解决方案都不起作用,但我重新安装了 jre7,现在它可以工作了。
回答by Dan Rayson
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.exefile 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.
- 打开regedit.exe
- 搜索“
jre6
”或按照以下步骤操作:HKLM->SOFTWARE->Classes->jarfile->shell->open->command
- 将数据字段更改为JDK 目录中javaw.exe文件的目录。
- 看在上帝的
[-jar "%1" %*]
份上,别忘了把奇怪的部分放在最后。 - 享受智胜 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.
当我JRE6
为 Chrome安装插件时,这发生在我身上,尽管JRE
我猜在安装任何其他插件时可能会发生这种情况。
回答by daver70
I fixed it by going to regedit and searched for jre which brought up...
我通过去 regedit 并搜索 jre 来修复它,它带来了......
HKEY_CURRENT_USER/Software/Classes/Applications/javaw.exe/shell/open/command
HKEY_CURRENT_USER/Software/Classes/Applications/javaw.exe/shell/open/command
I changed the REG_SZ
我改变了 REG_SZ
from: "C:\Program Files (x86)\Java\jre7\bin\javaw.exe" "%1"
来自:“C:\Program Files (x86)\Java\jre7\bin\javaw.exe”“%1”
to: "C:\Program Files (x86)\Java\jre7\bin\javaw.exe" -jar "%1" %*
到:"C:\Program Files (x86)\Java\jre7\bin\javaw.exe" -jar "%1" %*
回答by chubbsondubs
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:
当 Windows 要求您提供针对它运行的程序时,它不会-jar
在命令行中插入所需的参数。
如果它要求您提供exe
运行它的位置,那么这意味着不知何故您JRE
与.jar
扩展无关。您需要修改.jar
文件注册表项,使其运行c:\path_to_jre\bin\javaw -jar %1
才能正常工作。
它曾经在File Explorer > Tools > Folder Options > File Types
.
然后在那里寻找 .jar。您应该找到一个文本字段,显示要运行的可执行文件和命令行参数。这就是您要修改的内容。您也可以在注册表编辑器中执行此操作,但我忘记了HKEY
变量路径。我相信这个 KBA 会有所帮助:
回答by a_horse_with_no_name
Check if the file type jarfile
is actually registered with the *.jar extension using
检查文件类型jarfile
是否实际使用 *.jar 扩展名注册
assoc .jar
Seems like .jar is not linked to jarfile.
好像 .jar 没有链接到 jarfile。