简体   繁体   English

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

[英]How to run a jar file in a linux commandline

How to set the classpath to the current directory and also run the jar file named load.jar present in the current directory by providing the argument as load=2 from a linux command line.如何将类路径设置为当前目录,并通过从 linux 命令行提供参数为load=2来运行当前目录中名为load.jar的 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

Running a from class inside your JAR file load.jar is possible via可以通过以下方式在 JAR 文件load.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-Class tag.通常这是通过提供一个包含Main-Class标签的清单文件来完成的。 For documentation and examples have a look at this page .有关文档和示例,请查看此页面 The argument load=2 can be supplied like in a normal Java applications:可以像在普通 Java 应用程序中一样提供参数load=2

java -jar load.jar load=2

Having also the current directory contained in the classpath, required to also make use of the Class-Path tag.在类路径中还包含当前目录,还需要使用Class-Path标记。 See here for more information.请参阅此处了解更多信息。

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

The file will execute in terminal.该文件将在终端中执行。

Under linux there's a package called binfmt-support that 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!

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

In my case I had to utilize an additional flag- console to get it up and running:就我而言,我不得不使用一个额外的标志控制台来启动和运行它:

java -jar jarfilename.jar -console

The console flag was needed to run the file in shell and do instructions needed for setup like installation path and accept terms and conditions.需要控制台标志来在 shell 中运行文件并执行安装路径等设置所需的说明并接受条款和条件。

sudo -sH
java -jar filename.jar

Keep in mind to never run executable file in as root.请记住,永远不要以 root 身份运行可执行文件。

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

or all in one try this command:或合二为一试试这个命令:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM