Java 我可以通过 cmd 运行 .jar 文件,但我不能双击它们

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18434292/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 01:35:42  来源:igfitidea点击:

I can run .jar files through cmd, but I cannot double click them

javajarjavaw

提问by Lightfire228

First, I am studying Java, but I am still very new to it, so complicated code will go over my head.

首先,我正在学习 Java,但我对它仍然很陌生,所以复杂的代码会在我的脑海中浮现。

When I download a .jar file to say install a mod to minecraft and double click it, it doesn't run. The loading wheel goes for like two seconds and then stops, and nothing else happens. The .jar I know is set up right because it runs through cmd with the command

当我下载一个 .jar 文件说安装一个 mod 到 minecraft 并双击它时,它不会运行。装载轮运行了大约两秒钟,然后停止,没有其他任何事情发生。我知道的 .jar 设置正确,因为它使用命令通过 cmd 运行

    cd "wherever the file is"
    java -jar "file name".jar

So I've already tried to run it with the default program set as javaw.exe in the java\jdk1.7.0_25\jre\bin. Same where nothing happens. So the file association is set to the right place. I am at a loss here.

所以我已经尝试使用 java\jdk1.7.0_25\jre\bin 中设置为 javaw.exe 的默认程序运行它。什么都没有发生的地方。所以文件关联设置到了正确的位置。我在这里不知所措。

It isn't so bad since I can still run them through cmd, but when I get to the part in the book about jars, I want to be able to run it through the Windows Explorer GUI, which is like half the benefit of using .jar files that I can see.

还不错,因为我仍然可以通过 cmd 运行它们,但是当我进入书中关于 jars 的部分时,我希望能够通过 Windows Explorer GUI 运行它,这就像使用的好处的一半我可以看到的 .jar 文件。

Is it possibly a registry error? I have seen a lot of questions specifically about this problem, but most of them it seems were problems with setting up the .jar, which I didn't do, and I don't think that is the case here.

是否可能是注册表错误?我已经看到很多关于这个问题的问题,但其中大多数似乎是设置 .jar 的问题,我没有这样做,我认为这里不是这种情况。

I have Windows Vista 64bit.

我有 Windows Vista 64 位。

If I wasn't specific enough, feel free to ask.

如果我说的不够具体,请随时提问。

采纳答案by Meghan

I had the same problem, and it turns out that the .jar file association in the registry was broken. The following steps fixed it:

我遇到了同样的问题,结果是注册表中的 .jar 文件关联被破坏了。以下步骤修复了它:

Open the Registry Editor, and navigate to HKEY_CLASSES_ROOT\jarfile\shell\open\command. Modify the value of the Default key as follows: "[Location of your JRE]\bin\javaw.exe" -jar "%1" %*
Replace [Location of your JRE] with the root directory of your JRE installation.
For example:
"C:\Program Files\Java\jre7\bin\javaw.exe" -jar "%1" %*
Repeat the above steps for HKEY_LOCAL_MACHINE\SOFTWARE\Classes\jarfile\shell\open\command.

打开注册表编辑器,并导航到 HKEY_CLASSES_ROOT\jarfile\shell\open\command。如下修改 Default 键的值: "[您的 JRE 的位置]\bin\javaw.exe" -jar "%1" %*
将 [您的 JRE 的位置] 替换为您的 JRE 安装的根目录。
例如:
"C:\Program Files\Java\jre7\bin\javaw.exe" -jar "%1" %*
对 HKEY_LOCAL_MACHINE\SOFTWARE\Classes\jarfile\shell\open\command 重复上述步骤。

I have a Windows 7 machine, but I assume this would work for 32-bit environments as well.

我有一台 Windows 7 机器,但我认为这也适用于 32 位环境。

EDIT: Fixed the path in the example - it should use the "Program Files" directory and not "Program Files (x86)".

编辑:修复了示例中的路径 - 它应该使用“Program Files”目录而不是“Program Files (x86)”。

回答by Gaurav Varma

Right click the jar and open with winzip or winrar

右键单击 jar 并使用 winzip 或 winrar 打开

回答by Peter Rowntree

If you don't care too much about understanding the underlying problem, this might help:

如果您不太关心了解潜在问题,这可能会有所帮助:

http://johann.loefflmann.net/en/software/jarfix/index.html

http://johann.loefflmann.net/en/software/jarfix/index.html

--and a double-clickable .jar needs to have Main-Class correctly set in MANIFEST.MF

-- 并且一个可双击的 .jar 需要在 MANIFEST.MF 中正确设置 Main-Class

回答by JGutierrezC

You have to do RightClick on the jar file and select open with...then, Choose default program...(i think that's the correct translation for "Elegir programa predeterminado" in spanish). On the Open withdialog select Java(TM) Platform SE binary. If you don't see that option you should search for the javaw.exe in your java installation folder, and select it. Also you should check Use the selected program to open this kind of file

您必须右键单击 jar 文件,open with...然后选择Choose default program...(我认为这是西班牙语中“Elegir programa predeterminado”的正确翻译)。在Open with对话框中选择Java(TM) Platform SE binary。如果您没有看到该选项,您应该在您的 java 安装文件夹中搜索 javaw.exe,然后选择它。你也应该检查Use the selected program to open this kind of file

Best regards.

此致。

回答by user3523654

Just to reiterate - Meghan's answer fixed my problem of not being able to double-click to start a jar in Windows 7.

重申一下 - Meghan 的回答解决了我无法在 Windows 7 中双击启动 jar 的问题。

  • Open Registry editor

  • Navigate to HKEY_CLASSES_ROOT->jarfile->shell->open->command

  • Modify (Default) to "[Path to working JRE]/bin/javaw.exe" -jar "%1" %*

  • Make sure .jar files are opened by [Path to working JRE]/bin/javaw.exe by default

  • 打开注册表编辑器

  • 导航到 HKEY_CLASSES_ROOT->jarfile->shell->open->command

  • 修改(默认)为 "[Path to working JRE]/bin/javaw.exe" -jar "%1" %*

  • 确保 .jar 文件默认由 [Path to working JRE]/bin/javaw.exe 打开

回答by Wong KL

According to Johann Loefflmann, the root cause for the problem above is, that a program has stolen the .jar association. And he is right! Just follow the link below and install a small file JARFIX and the association will be set back and you can double click any jar file and run it in windows. It works for me (I have the same problem and before these I tried all solutions mentioned in the forum and failed).

根据 Johann Loefflmann 的说法,上述问题的根本原因是某个程序窃取了 .jar 关联。他是对的!只需按照下面的链接安装一个小文件 JARFIX,关联就会恢复,您可以双击任何 jar 文件并在 Windows 中运行它。它对我有用(我有同样的问题,在这些之前我尝试了论坛中提到的所有解决方案但都失败了)。

https://johann.loefflmann.net/en/software/jarfix/index.html

https://johann.loefflmann.net/en/software/jarfix/index.html