Java 从批处理文件运行 .jar
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2622062/
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
Run .jar from batch-file
提问by Arivu2020
I have created an executable .jar
file. How can I execute the .jar
using a batch-file without mentioning a class path?
我已经创建了一个可执行.jar
文件。如何在.jar
不提及类路径的情况下使用批处理文件执行?
回答by RichieHindle
If double-clicking the .jar file in Windows Explorer works, then you should be able to use this:
如果在 Windows 资源管理器中双击 .jar 文件有效,那么您应该能够使用它:
start myapp.jar
in your batch file.
在您的批处理文件中。
The Windows start
command does exactly the same thing behind the scenes as double-clicking a file.
Windowsstart
命令在后台执行与双击文件完全相同的操作。
回答by BalusC
Just the same way as you would do in command console. Copy exactly those commands in the batch file.
就像在命令控制台中所做的一样。在批处理文件中准确复制这些命令。
回答by mdm
To run a .jar
file from the command line, just use:
要从命令行运行.jar
文件,只需使用:
java -jar YourJar.jar
To do this as a batch file, simply copy the command to a text file and save it as a .bat
:
要作为批处理文件执行此操作,只需将命令复制到文本文件并将其另存为.bat
:
@echo off
java -jar YourJar.jar
The @echo off
just ensures that the second command is not printed.
的@echo off
只是确保不打印所述第二命令。
回答by Jonno_FTW
If you want a batch file to run a jar file, make a blank file called runjava.bat with the contents:
如果您希望批处理文件运行 jar 文件,请使用以下内容创建一个名为 runjava.bat 的空白文件:
java -jar "C:\myjarfile.jar"
回答by Venkat
java -jar "C:\myjarfile.jar"
You might need to add "\\"
to the command. Try this!
您可能需要添加"\\"
到命令中。尝试这个!
回答by Jleuleu
On Windows you can use the following command.
在 Windows 上,您可以使用以下命令。
start javaw -jar JarFile.jar
By doing so, the Command Prompt Window doesn't stay open.
通过这样做,命令提示符窗口不会保持打开状态。
回答by merlin2011
My understanding of the question is that the OP is trying to avoid specifying a class-path in his command line. You can do this by putting the class-path in the Manifest file.
我对这个问题的理解是 OP 试图避免在他的命令行中指定类路径。您可以通过将类路径放在清单文件中来做到这一点。
In the manifest:
在清单中:
Class-Path: Library.jar
This document gives more details:
本文档提供了更多详细信息:
http://docs.oracle.com/javase/tutorial/deployment/jar/downman.html
http://docs.oracle.com/javase/tutorial/deployment/jar/downman.html
To create a jar using the a manifest file named MANIFEST, you can use the following command:
要使用名为 MANIFEST 的清单文件创建 jar,您可以使用以下命令:
jar -cmf MANIFEST MyJar.jar <class files>
If you specify relative class-paths (ie, other jars in the same directory), then you can move the jar's around and the batch file mentioned in mdm's answer will still work.
如果您指定相对类路径(即同一目录中的其他 jar),那么您可以移动 jar 并且 mdm 的答案中提到的批处理文件仍然有效。
回答by BrunoMedeiros
There is a solution to this that does not require to specify the path of the jar file inside the .bat. This means the jar can be moved around in the filesystem with no changes, as long as the .bat file is always located in the same directory as the jar. The .bat code is:
有一个解决方案,不需要在 .bat 中指定 jar 文件的路径。这意味着 jar 可以在文件系统中移动而无需更改,只要 .bat 文件始终与 jar 位于同一目录中。.bat 代码是:
java -jar %~dp0myjarfile.jar %*
Basically %0
would expand to the .bat full path, and %~dp0
expands to the .bat full path except the filename. So %~dp0myjarfile.jar
is the full path of the myjarfile.jar colocated with the .bat file. %*
will take all the arguments given to the .bat and pass it to the Java program. (see: http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/percent.mspx?mfr=true)
基本上%0
会扩展到 .bat 完整路径,并%~dp0
扩展到除文件名之外的 .bat 完整路径。因此,%~dp0myjarfile.jar
与.bat文件位于同一位置的myJarFile.jar中的完整路径。%*
将获取给 .bat 的所有参数并将其传递给 Java 程序。(参见:http: //www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/percent.mspx?mfr=true)
回答by User123456
cd "Your File Location without inverted commas"
example :cd C:\Users*****\Desktop\directory\target
例如:cd C:\Users*****\Desktop\directory\target
java -jar myjar.jar
example bat file looks like this:
示例 bat 文件如下所示:
@echo OFF
cd C:\Users\****\Desktop\directory\target
java -jar myjar.jar
This will work fine.
这将正常工作。
回答by Burhan ARAS
you shoult try this one :
你应该试试这个:
java -cp youJarName.jar your.package.your.MainClass