我有一个名为helloworld.jar的JAR文件。 为了运行它,我在命令行窗口中执行以下命令:
java -jar helloworld.jar
这工作得很好,但我如何执行它与双击? 我需要安装什么软件吗?
我有一个名为helloworld.jar的JAR文件。 为了运行它,我在命令行窗口中执行以下命令:
java -jar helloworld.jar
这工作得很好,但我如何执行它与双击? 我需要安装什么软件吗?
当前回答
处方:如果你的提示符出现并立即消失,那么它这样做的原因是你的程序被执行并自动关闭。尝试在最后放置一个扫描器来终止,它会让您的提示符在终止之前等待输入。(或者使用延迟)
在完全相同的情况下,从cmd运行.jar工作正常,但双击没有任何作用。
解决方案: 打开任何文本编辑器并编写命令行: java -jar示例 将文件保存为.bat文件。 运行这个bat文件以获得所需的输出。
更进一步,您可以使用简单的GUI工具(如bat to exe Converter)将此bat文件转换为exe文件。
现在你可以把你的.jar文件以。exe文件的形式共享,任何人都可以使用,只要确保你把所有文件放在一起。(特别是.jar和.bat文件,因为.bat只是一个cmd提示符)(感觉如何逻辑)
对于开发和学习,我还是个新手。如有错误,请原谅。欢迎提出建议。
其他回答
最简单的方法可能是升级或重新安装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
有一种方法不需要用户在他的PC上做改变。runtime . getrtime .exec()允许我们启动cmd.exe并在其中执行命令。因此,当用户单击.jar文件时,java程序可以在命令提示符下自行运行。
public static void main(String[] args) throws IOException {
if(args.length == 0) {
Process p = Runtime.getRuntime().exec("cmd.exe /c start java -jar " + (new File(NameOfClass.class.getProtectionDomain().getCodeSource().getLocation().getPath())).getAbsolutePath() + " cmd");
} else {
//code to be executed
}
}
在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
使用.bat文件:
将命令放在.bat文件中。在这里,你的命令将是java -jar path\yourJarName.jar。 比如:java -jar C:\workspace\myApplication.jar
保存它,双击bat文件运行你的jar。
不幸的是,这并不容易,因为微软在最近的Windows版本中删除了高级文件关联对话框。-对于较新的Windows版本,您可以只指定将用于打开.jar文件的应用程序。
修复在Windows上打开.jar文件需要两个步骤。
Open the Control Panel, and chose "Default Programs -> Set Associations". Find .jar extension (Executable JAR file) there, and pick Java as default program to open this extension. It will probably be listed as "Java Platform(SE)". A faster alternative perhaps is straightforward right-click on a .jar file, and then change associated program by clicking on the "Change..." button. Now open the regedit, and open the HKEY_CLASSES_ROOT\jarfile\shell\open\command key. Luckilly for us, we may specify parameters there for the (Default) value. On my Windows system it looks like: C:\app\32\jre7\bin\javaw.exe" -jar "%1" %* but in most cases it is the following string: C:\Program Files\Java\jre7\bin\javaw.exe" -jar "%1" %*
注:
不要在那里使用java.exe,因为它会打开shell窗口。 本线程中提到的jarfix工具很可能只是为您修改注册表。我更喜欢手动注册表更改方法,因为这意味着系统管理员可以将注册表更改“推送”到网络中的所有工作站。