简体   繁体   English

Java可以从cmd运行jar,但不能通过双击运行

[英]java can run jar from cmd but not by double clicking

I just created a jar file using jdk7. 我刚刚使用jdk7创建了一个jar文件。 I tried running it but kept getting an error. 我尝试运行它,但一直出现错误。 It turned out it was pointing at a jre6 and not the jre in jdk7. 原来,它指向的是jre6,而不是jdk7中的jre。 So, I used the following command to change it 因此,我使用以下命令对其进行了更改

ftype jarfile = "C:\\path to jre\\bin\\javaw.exe" -jar "%1" %* ftype jarfile =“ C:\\ 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. 在那之后,我试图再次双击我的jar文件,但是它弹出了一个窗口,要求我选择一个打开它的程序。 When I chose the same javaw.exe file as above it runs but exits immediately without doing anything. 当我选择与上述相同的javaw.exe文件时,它将运行,但不执行任何操作便立即退出。 I can run it fine in cmd by doing the java -jar file.jar command. 我可以通过执行java -jar file.jar命令在cmd中很好地运行它。 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. 我什至尝试删除.jar注册表项,但这使我回到要求我选择要打开的程序的位置。 I'm running windows 7 ultimate if that matters. 如果重要的话,我正在运行Windows 7 Ultimate。

EDIT: In the registry there are 3 subkeys: OpenWithList, OpenWithProgids, and UserChoice. 编辑:在注册表中有3个子项:OpenWithList,OpenWithProgids和UserChoice。 I've changed all the defaults to the path above except UserChoice which won't let me edit it from regedit. 除了UserChoice之外,我将所有默认值都更改为上述路径,后者不允许我从regedit中对其进行编辑。 When I delete that and try to run the jar file, it opens up the choose a program to open with window again. 当我删除该文件并尝试运行jar文件时,它会打开选​​择程序以再次打开窗口。 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. 好的,我在其他地方找到了答案,但是又遇到了这个问题,因此通过Google来到了这里。
The solution is... 解决方法是...

  1. Open up regedit.exe 打开regedit.exe
  2. Either search for " jre6 " or follow this: 搜索“ jre6 ”或遵循以下步骤:
    HKLM->SOFTWARE->Classes->jarfile->shell->open->command
  3. Change the Data field to the directory of the javaw.exe file that's in your JDK directory. 将数据字段更改为JDK目录中的javaw.exe文件的目录。
  4. For the love of God, don't forget to put the weird [-jar "%1" %*] parts on the end of it. 对于上帝的爱,别忘了将奇怪的[-jar "%1" %*]部分放在结尾。
  5. Enjoy outsmarting Oracle. 享受超越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. 当我为Chrome安装JRE6插件时,这发生在我身上,尽管我猜安装其他JRE时也可能发生。

I fixed it by going to regedit and searched for jre which brought up... 我通过注册进行修复并搜索了出现的jre ...

HKEY_CURRENT_USER/Software/Classes/Applications/javaw.exe/shell/open/command HKEY_CURRENT_USER /软件/类/应用程序/javaw.exe/shell/打开/命令

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”%*

When windows asks you for a program to run it against it won't insert the needed -jar argument in the command line. 当Windows要求您针对它运行程序时,不会在命令行中插入所需的-jar参数。

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. 如果它询问您要在其中运行它的exe ,那么这意味着您的JRE.jar扩展名无关。 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. 您需要修改.jar文件注册表项,以便它运行c:\\path_to_jre\\bin\\javaw -jar %1才能起作用。
It used to be under File Explorer > Tools > Folder Options > File Types . 它曾经位于“ File Explorer > Tools > Folder Options > File Types

Then look for .jar in there. 然后在其中寻找.jar。 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. 您也可以在注册表编辑器中执行此操作,但是我忘记了HKEY变量路径。 I'm sure this KBA will help: 我相信此KBA会有所帮助:

support_microsoft_kb_950505 support_microsoft_kb_950505

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。

我不确定为什么所有解决方案都不起作用,但是我重新安装了jre7,它现在​​可以工作了。

Isn't it easier to change your java version to you rnew jdk7 by changing the CLASSPATH environment variable? 通过更改CLASSPATH环境变量将Java版本更改为rnew jdk7难道不是很容易吗? See how here 看看这里如何

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM