我有一个名为helloworld.jar的JAR文件。 为了运行它,我在命令行窗口中执行以下命令:

java -jar helloworld.jar

这工作得很好,但我如何执行它与双击? 我需要安装什么软件吗?


当前回答

另一种通过单击/双击运行jar文件的方法是在 文件的名字。例如,您可以将文件MyJar.jar重命名为-jar MyJar.jar。

当然,您必须拥有与java.exe相关联的.class文件。这可能不是在所有情况下都有效,但对我来说大多数时候都有效。

其他回答

在Windows XP *中,你只需要2个shell命令:

   C:\>ftype myjarfile="C:\JRE1.6\bin\javaw.exe" -jar "%1" %* 
   C:\>assoc .jar=myjarfile  

显然使用正确的JRE路径和任何你想要的名称,而不是myjarfile。

检查当前设置:

   C:\>assoc .jar  
   C:\>ftype jarfile  

这次使用第一个命令返回的值(如果有的话),而不是jarfile。

*未在Windows 7上测试

如果使用eclipse生成java文件,可以选择将其导出为可运行的jar文件。我用我的程序做了这个,我只要点击罐子,它就会像这样运行。这将工作在两个windows,以及os x。

最简单的方法可能是升级或重新安装Java运行时环境(JRE)。

或:

Open the Windows Explorer, from the Tools select 'Folder Options...' Click the File Types tab, scroll down and select JAR File type. Press the Advanced button. In the Edit File Type dialog box, select open in Actions box and click Edit... Press the Browse button and navigate to the location the Java interpreter javaw.exe. In the Application used to perform action field, needs to display something similar to C:\Program Files\Java\j2re1.4.2_04\bin\javaw.exe" -jar "%1" % (Note: the part starting with 'javaw' must be exactly like that; the other part of the path name can vary depending on which version of Java you're using) then press the OK buttons until all the dialogs are closed.

这是从这里偷来的:http://windowstipoftheday.blogspot.com/2005/10/setting-jar-file-association.html

我正在运行Windows 7 x64,无法使用任何这些修复程序。

这条对我来说还是有用的:

http://thepanz.netsons.org/post/windows7-jar-file-association-broken-with-nokia-ovi

你可以下载一个包含要运行的.bat文件的存档文件,但请检查实际的javaw.exe!!!!的路径

在Windows 10上也有同样的问题 解决方案:

检查您的JAVA_HOME和JAVA_PATH。 https://javatutorial.net/set-java-home-windows-10 使用Jarfix恢复.jar和javaw.exe之间的关联 https://johann.loefflmann.net/en/software/jarfix/index.html