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

java -jar helloworld.jar

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


当前回答

在windows上运行.jar文件有很多方法。其中一个正在使用命令提示符。

步骤:

打开命令提示符(以管理员身份运行) 现在为根目录写入“cd\”命令 输入“java jar文件名。jar” 注意:你也可以使用任何第三方应用程序,如WinRAR, jarfix等。

其他回答

在windows上运行.jar文件有很多方法。其中一个正在使用命令提示符。

步骤:

打开命令提示符(以管理员身份运行) 现在为根目录写入“cd\”命令 输入“java jar文件名。jar” 注意:你也可以使用任何第三方应用程序,如WinRAR, jarfix等。

有一种方法不需要用户在他的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
    }
}

如果需要通过双击jar文件来运行该jar文件,则必须将其创建为“Runnable 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上找到

你需要检查一些东西;如果这是您自己的jar文件,请确保在清单中定义了main类。因为我们知道您可以从命令行运行它,所以要做的另一件事是创建一个windows快捷方式,并修改属性(您必须四处查看,我没有windows机器可以查看),以便它在打开时执行的命令是您提到的java -jar命令。

另一件事是:如果某个东西没有混淆,它就应该工作;检查并确保已将Java与.jar扩展名关联。

最简单的方法可能是升级或重新安装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