java在windows上运行jar文件

jum4pzuy  于 2021-07-06  发布在  Java
关注(0)|答案(22)|浏览(585)

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

java -jar helloworld.jar

这很好,但是如何用双击来执行它呢?我需要安装软件吗?

4dbbbstv

4dbbbstv16#

在regedit中,打开 HKEY_CLASSES_ROOT\Applications\java.exe\shell\open\command 双击左边的default并在 java.exe “路径和路径” %1 “论点。

0kjbasz6

0kjbasz617#

有一种方法不需要用户在pc上做更改。runtime.getruntime.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
    }
}
3qpi33ja

3qpi33ja18#

我´我正在运行Windows7x64,无法使用这些修复程序。
这一个毕竟对我有用:
http://thepanz.netsons.org/post/windows7-jar-file-association-broken-with-nokia-ovi
有一个档案,你可以下载包含一个.bat文件运行,但检查路径的实际javaw.exe!!!!

6rvt4ljy

6rvt4ljy19#

通过单击/双击运行jar文件的另一种方法是在文件名前加“-jar”。例如,您可以重命名该文件 MyJar.jar-jar MyJar.jar .
你一定有 .class 与关联的文件 java.exe ,当然。这可能不是在所有情况下都有效,但对我来说大多数时候都有效。

2w3rbyxf

2w3rbyxf20#

如果您需要通过双击jar文件来运行它,那么必须将其创建为“可运行jar”。您只需使用ide就可以做到这一点。
如果您使用的是eclipse,请执行以下步骤:

To create a new runnable JAR file in the workbench:

1.From the menu bar's File menu, select Export.
2.Expand the Java node and select Runnable JAR file. Click Next.
3.In the  Opens the Runnable JAR export wizard Runnable JAR File Specification page, select a 'Java Application' launch configuration to use to create a runnable JAR.
4.In the Export destination field, either type or click Browse to select a location for the JAR file.
5.Select an appropriate library handling strategy.
Optionally, you can also create an ANT script to quickly regenerate a previously created runnable JAR file.

更多信息可以在eclipse帮助页上找到:link

bfhwhh0e

bfhwhh0e21#

cmd(命令提示符)上的第一个设置路径:

set path="C:\Program Files\Java\jre6\bin"

然后键入

java -jar yourProgramname.jar
bq8i3lrv

bq8i3lrv22#

你需要检查一些东西;如果这是您自己的jar文件,请确保您在清单中定义了一个主类。因为我们知道您可以从命令行运行它,所以要做的另一件事是创建一个windows快捷方式,并修改属性(您必须四处查看,我没有windows机器可以查看),以便它在open时执行的命令就是您提到的java-jar命令。
另一件事:如果某件事没有被混淆,它无论如何都应该起作用;检查并确保java与.jar扩展相关联。

相关问题