Java 如何创建一个 cmd 文件来运行 jars?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18811974/
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
How to create a cmd file to run jars?
提问by Stefan Carlson
I have literally tried everything to try and make my jar files executable by double clicking. But i have come to the conclusion that either I need some major help because my java installation has a problem, or I need to create a .cmd file to automatically run them properly. The code in the file would be like this:
我已经尝试了所有方法来尝试通过双击使我的 jar 文件可执行。但我得出的结论是,要么我需要一些主要的帮助,因为我的 java 安装有问题,要么我需要创建一个 .cmd 文件来自动正确运行它们。文件中的代码如下所示:
java -jar myfile.jar
What would i replace myfile.jar
with so that windows puts in the file extension of what i'm trying to open? Thanks.
我会用什么替换myfile.jar
,以便 Windows 放入我要打开的文件扩展名?谢谢。
采纳答案by A4L
If you want to have a .cmd
file to run your jar, then such a file could look like this
如果你想有一个.cmd
文件来运行你的 jar,那么这样的文件可能看起来像这样
@ECHO OFF
SET JRE_HOME=<path to your jre>
%JRE_HOME%\bin\java.exe -jar myfile.jar
Note that here the option -jar
that implicitly means that myfile.jar
contains all your dependencies and you cannot extend the classpath to include other dependencies. Also using the this option require your mainifest to have the attibute Main-Class
which tells which class to run / is the entry point for your program.
请注意,此处-jar
隐式意味着myfile.jar
包含所有依赖项的选项,并且您不能扩展类路径以包含其他依赖项。同样使用此选项需要您的 mainifest 具有指示Main-Class
运行哪个类的属性/是您程序的入口点。
Better yet include myfile.jar
in the classpath an pass your main class to java.exe
最好myfile.jar
在类路径中包含一个将您的主类传递给java.exe
@ECHO OFF
SET JRE_HOME=<path to your jre>
SET MY_CLASSPATH=<jars/libs your app depends on separated by semicolon>;myfile.jar
%JRE_HOME%\bin\java.exe -cp %MY_CLASSPATH% <your main class>
Finally if you want to make an .exe
of your java programm then you might want to use a wrapper
like jsmoothwhich bundles your jar and all it's dependencies into a single .exe
file
最后,如果您想制作一个.exe
Java 程序,那么您可能需要使用wrapper
类似jsmooth 的工具,它将您的 jar 及其所有依赖项捆绑到一个.exe
文件中
回答by Hari Sri
create a batch file open note pad type the following
创建一个批处理文件打开记事本键入以下内容
set path="...<where u install java>/bin"
java -jar myfile.jar
save the notepad as filename.bat and put it in folder containing jar file double click it automatically run
将记事本另存为 filename.bat 并放入包含 jar 文件的文件夹中双击它自动运行
just for example i give you a sample
举个例子,我给你一个样本
@echo off
set path="F:/java/bin"
java -jar myfile.jar
pause
save it as filename.bat put it in a folder that contain filename.jar
将其另存为 filename.bat 将其放入包含 filename.jar 的文件夹中
if u can't success in above try this in batch file (it work if you install java runtime (jre)
如果您无法在上面成功,请在批处理文件中尝试此操作(如果您安装 java 运行时(jre),它会起作用)
set path =c:\Program Files\Java\jre7\bin\javaw.exe
java -jar ImageEditor.jar
pause
ImageEditor is jar file note both jar and bat file must be in same folder
ImageEditor 是 jar 文件 注意 jar 和 bat 文件必须在同一个文件夹中
回答by Stefan Carlson
I Reinstalled Java, and now it works fine. Apparently, I had set WinRAR to be the default Jar opener for Minecraft modding. Re-installing Java reset the .jar extension, making the JRE the default instead of WinRAR.
我重新安装了 Java,现在它工作正常。显然,我已经将 WinRAR 设置为 Minecraft 模组的默认开罐器。重新安装 Java 会重置 .jar 扩展名,使 JRE 成为默认值而不是 WinRAR。