Java 如何在 Linux 命令行中运行 jar 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19904810/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 21:12:59  来源:igfitidea点击:

How to run a jar file in a linux commandline

java

提问by Dark Matter

How to set the classpath to the current directory and also run the jar file named load.jarpresent in the current directory by providing the argument as load=2from a linux command line.

如何将类路径设置为当前目录,并load.jar通过load=2从 linux 命令行提供参数来运行当前目录中存在的名为 jar 文件。

I did try to run the jar as follows but its executing classes from some other directory.

我确实尝试按如下方式运行 jar,但它从其他目录执行类。

java -cp ./load.jar:$CLASSPATH load.Start load=2

回答by Stefan Freitag

Running a from class inside your JAR file load.jaris possible via

load.jar可以通过以下方式在 JAR 文件中运行 from 类

java -jar load.jar

When doing so, you have to define the application entry point. Usually this is done by providing a manifest file that contains the Main-Classtag. For documentation and examples have a look at this page. The argument load=2can be supplied like in a normal Java applications:

这样做时,您必须定义应用程序入口点。通常这是通过提供包含Main-Class标签的清单文件来完成的。有关文档和示例,请查看此页面load=2可以像在普通 Java 应用程序中一样提供参数:

java -jar load.jar load=2

Having also the current directory contained in the classpath, required to also make use of the Class-Pathtag. See herefor more information.

在类路径中还包含当前目录,也需要使用Class-Path标记。请参阅此处了解更多信息。

回答by Ashraf Abusada

For example to execute from terminal (Ubuntu Linux) or even (Windows console) a java file called filex.jar use this command:

例如,要从终端(Ubuntu Linux)甚至(Windows 控制台)执行名为 filex.jar 的 java 文件,请使用以下命令:

java -jar filex.jar

java -jar filex.jar

The file will execute in terminal.

该文件将在终端中执行。

回答by Aaditya Purani

sudo -sH
java -jar filename.jar

Keep in mind to never run executable file in as root.

请记住,永远不要以 root 身份运行可执行文件。

回答by Bruno Bieth

Under linux there's a package called binfmt-supportthat allows you to run directly your jar without typing java -jar:

在 linux 下有一个包binfmt-support,它允许你直接运行你的 jar 而不用输入java -jar

sudo apt-get install binfmt-support
chmod u+x my-jar.jar
./my-jar.jar # there you go!

回答by Paul

copy your file in linux Java directory

将您的文件复制到 linux Java 目录中

cp yourfile.jar /java/bin

open the directory

打开目录

cd /java/bin

and execute your file

并执行你的文件

./java -jar yourfile.jar

./java -jar yourfile.jar

or all in one try this command:

或合二为一试试这个命令:

/java/bin/java -jar jarfilefolder/jarfile.jar

/java/bin/java -jar jarfilefolder/jarfile.jar

回答by Magnus Melwin

For OpenSuse Linux, One can simply install the java-binfmt package in the zypper repository as shown below:

对于 OpenSuse Linux,可以简单地在 zypper 存储库中安装 java-binfmt 包,如下所示:

sudo zypper in java-binfmt-misc
chmod 755 file.jar
./file.jar