Java 在 Windows 上运行 JAR 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/394616/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Running JAR file on Windows
提问by DonX
I have a JAR file named helloworld.jar. In order to run it, I'm executing the following command in a command-line window:
我有一个名为helloworld.jar的 JAR 文件。为了运行它,我在命令行窗口中执行以下命令:
java -jar helloworld.jar
This works fine, but how do I execute it with double-click instead? Do I need to install any software?
这工作正常,但我如何通过双击来执行它?我需要安装任何软件吗?
采纳答案by Brian Kelly
Easiest route is probably upgrading or re-installing the Java Runtime Environment (JRE).
最简单的方法可能是升级或重新安装 Java 运行时环境 (JRE)。
Or this:
或这个:
- 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.
- 打开 Windows 资源管理器,从工具中选择“文件夹选项...”
- 单击文件类型选项卡,向下滚动并选择 JAR 文件类型。
- 按高级按钮。
- 在编辑文件类型对话框中,选择在操作中打开并单击编辑...
- 按浏览按钮并导航到 Java 解释器 javaw.exe 的位置。
- 在用于执行操作的应用程序字段中,需要显示类似于
C:\Program Files\Java\j2re1.4.2_04\bin\javaw.exe" -jar "%1" %
(注意:以 'javaw' 开头的部分必须完全相同;路径名的另一部分可能会因您使用的 Java 版本而异) 然后按 OK 按钮直到所有对话框都关闭。
Which was stolen from here: http://windowstipoftheday.blogspot.com/2005/10/setting-jar-file-association.html
这是从这里被盗的:http: //windowstipoftheday.blogspot.com/2005/10/setting-jar-file-association.html
回答by Charlie Martin
You want to check a couple of things; if this is your own jar file, make sure you have defined a Main-class in the manifest. Since we know you can run it from the command line, the other thing to do is create a windows shortcut, and modify the properties (you'll have to look around, I don't have a Windows machine to look at) so that the command it executes on open is the java -jar command you mentioned.
你想检查几件事;如果这是您自己的 jar 文件,请确保您已在清单中定义了 Main-class。因为我们知道您可以从命令行运行它,所以要做的另一件事是创建一个 Windows 快捷方式,并修改属性(您必须环顾四周,我没有 Windows 机器可以查看),以便它在打开时执行的命令是您提到的 java -jar 命令。
The other thing: if something isn't confused, it should work anyway; check and make sure you have java associated with the .jar extension.
另一件事:如果某些东西没有被混淆,它无论如何都应该起作用;检查并确保您有与 .jar 扩展名关联的 java。
回答by Brian Kelly
Besides all of the other suggestions, there is one other thing you need to consider. Is your helloworld.jar a console program? If it is, then I don't believe you'll be able to make it into a double-clickable jar file. Console programs use the regular cmd.exe shell window for their input and output. Usually the jar "launcher" is bound to javaw.exe which doesn't create a command-shell window.
除了所有其他建议之外,您还需要考虑另一件事。你的 helloworld.jar 是一个控制台程序吗?如果是这样,那么我不相信您能够将其制作成可双击的 jar 文件。控制台程序使用常规的 cmd.exe shell 窗口进行输入和输出。通常,jar“启动器”绑定到 javaw.exe,它不会创建命令外壳窗口。
回答by Bob
An interesting side effect of this causes a problem when starting runnable jar files in the command prompt.
在命令提示符下启动可运行的 jar 文件时,一个有趣的副作用会导致出现问题。
If you try (in a command prompt):
如果您尝试(在命令提示符中):
jarfile.jar parameter
No joy, because this is being translated to the following (which doesn't work):
不高兴,因为这被翻译成以下内容(不起作用):
javaw.exe -jar jarfile.jar parameter
However, the following command does work:
但是,以下命令确实有效:
java.exe -jar jarfile.jar parameter
If you change the association in file manager as described above to:
如果如上所述将文件管理器中的关联更改为:
"C:\Program Files\Java\j2re1.4.2_04\bin\java.exe" -jar "%1" %*
Then you can type:
然后你可以输入:
jarfile.jar parameter
in the command prompt and it will now work!
在命令提示符下,它现在可以工作了!
EDIT:(However you then get a black console window when you run a form based (non console) Java app, so this is not an ideal solution)
编辑:(但是,当您运行基于表单的(非控制台)Java 应用程序时,您会得到一个黑色的控制台窗口,因此这不是理想的解决方案)
If you run these jar files by double clicking them in windows, no parameters will be passed so your Java code needs to handle the stack overflow exception and include a "press a key" function at the end or the window will just disappear.
如果您通过在 Windows 中双击它们来运行这些 jar 文件,则不会传递任何参数,因此您的 Java 代码需要处理堆栈溢出异常并在最后包含一个“按键”功能,否则窗口将消失。
In order to pass a parameter in windows you have to create a shortcut to the jar file, which includes the parameter in the target line (right click on the shortcut and select properties) you can not add parameters to the jar file icon itself in this way.
为了在 Windows 中传递参数,您必须创建一个到 jar 文件的快捷方式,其中包括目标行中的参数(右键单击快捷方式并选择属性),您不能在此向 jar 文件图标本身添加参数道路。
There isn't a single, consistent solution here, but you would have the same problem with any other console application.
这里没有单一的、一致的解决方案,但您在使用任何其他控制台应用程序时都会遇到同样的问题。
There is a windows freeware application called "bat to exe" which you can use to create an exe file from a .bat file with the apropriate command line in it. you can also embed the jar file in the exe with this application, and make it clean it up when it has finished running, so this may be a more elegant solution.
有一个名为“bat to exe”的 Windows 免费软件应用程序,您可以使用它从带有适当命令行的 .bat 文件创建一个 exe 文件。您也可以将这个jar文件嵌入到这个应用程序的exe中,并在它运行完成后将其清理干净,所以这可能是一个更优雅的解决方案。
回答by Hendy Irawan
In Windows Vista or Windows 7, the manual file association editorhas been removed.
在 Windows Vista 或 Windows 7 中,手动文件关联编辑器已被删除。
The easiest way is to run Jarfix, a tiny but powerful freeware tool. Just run it and your Java apps is back... double-clickable again.
最简单的方法是运行Jarfix,这是一个小巧但功能强大的免费软件工具。只需运行它,您的 Java 应用程序就回来了……再次双击。
回答by user85421
In Windows XP *you need just 2 shellcommands:
在 Windows XP * 中,您只需要 2 个shell命令:
C:\>ftype myjarfile="C:\JRE1.6\bin\javaw.exe" -jar "%1" %*
C:\>assoc .jar=myjarfile
obviously using the correct path for the JRE and any name you want instead of myjarfile
.
显然使用正确的 JRE 路径和任何您想要的名称而不是myjarfile
.
To just check the current settings:
只检查当前设置:
C:\>assoc .jar
C:\>ftype jarfile
this time using the value returned by the first command, if any, instead of jarfile
.
这次使用第一个命令返回的值(如果有)而不是jarfile
.
*not tested with Windows 7
*未在 Windows 7 上测试
回答by Pusu
If you have a jar file called Example.jar, follow these rules:
如果您有一个名为 Example.jar 的 jar 文件,请遵循以下规则:
- Open a
notepad.exe
- Write :
java -jar Example.jar
- Save it with the extension
.bat
- Copy it to the directory which has the
.jar
file - Double click it to run your
.jar
file
- 打开一个
notepad.exe
- 写 :
java -jar Example.jar
- 用扩展名保存
.bat
- 将其复制到包含该
.jar
文件的目录 - 双击它运行你的
.jar
文件
回答by Demorf
If you need to distribute your .jar file and make it runnable at other people's Windows computers, you can make a simple .bat file like this in the command prompt:
如果您需要分发您的 .jar 文件并使其可在其他人的 Windows 计算机上运行,您可以在命令提示符下创建一个简单的 .bat 文件,如下所示:
java -jar MyJavaTool.jar
and place the .bat file in the same directory as your .jar file.
并将 .bat 文件放在与 .jar 文件相同的目录中。
回答by lawid
I′m running Windows 7 x64 and was unable to use any of these fixes.
我正在运行 Windows 7 x64 并且无法使用任何这些修复程序。
This one worked for me afterall:
毕竟这个对我有用:
http://thepanz.netsons.org/post/windows7-jar-file-association-broken-with-nokia-ovi
http://thepanz.netsons.org/post/windows7-jar-file-association-broken-with-nokia-ovi
There is an archive which you can download containing a .bat file to run, but check the path of the actual javaw.exe!!!!
有一个存档,您可以下载包含要运行的 .bat 文件,但请检查实际 javaw.exe 的路径!!!!
回答by suraj
First set path on cmd(command prompt):
首先在cmd(命令提示符)上设置路径:
set path="C:\Program Files\Java\jre6\bin"
then type
然后输入
java -jar yourProgramname.jar