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

java -jar helloworld.jar

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


当前回答

编译:

javac  -cp ".;./mysql-connector-java-5.0.8.jar;mybatis-3.0.1.jar;ibatis-2.3.0.677.jar" MainStart.java

为运行:

java  -cp ".;./mysql-connector-java-5.0.8.jar;mybatis-3.0.1.jar;ibatis-2.3.0.677.jar" MainStart

其他回答

如果需要通过双击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文件的方法是在 文件的名字。例如,您可以将文件MyJar.jar重命名为-jar MyJar.jar。

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

不幸的是,这并不容易,因为微软在最近的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工具很可能只是为您修改注册表。我更喜欢手动注册表更改方法,因为这意味着系统管理员可以将注册表更改“推送”到网络中的所有工作站。

在regedit中打开HKEY_CLASSES_ROOT\Applications\java.exe\shell\open\命令

双击左边的default,在java.exe路径和“%1”参数之间添加-jar。

除了所有其他的建议,还有一件事你需要考虑。你的helloworld.jar是一个控制台程序吗?如果是,那么我不相信您能够将其变成一个可双击的jar文件。控制台程序使用常规的cmd.exe shell窗口作为它们的输入和输出。通常jar“启动器”绑定到javaw.exe,它不会创建命令shell窗口。