在 Linux 中,如何使用外部 jar 文件执行 Java jar 文件?

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

In Linux, how to execute Java jar file with external jar files?

javalinuxjarexecution

提问by Gowtham

In Linux, how to execute Java jar file with external jar files?

在 Linux 中,如何使用外部 jar 文件执行 Java jar 文件?

回答by Jigar Joshi

java -jar /path/to/externalJarFile.jar

Update

更新

You can add the required library in manifest with Class-Path:header

您可以使用Class-Path:标头在清单中添加所需的库

For example :

例如 :

Class-Path: MyUtils.jar

See

回答by Sean Patrick Floyd

Either use the -cpflag:

要么使用-cp标志:

java -cp /path/to/somefolder/*.jar:/path/to/otherfolder/*.jar com.YourMainClass

Or add a Class-Path:header to your jar's manifest (see Jigar's answer)

或者Class-Path:在你的 jar 的清单中添加一个标题(见 Jigar 的回答)



Noteto others who answered with java -jar <etc>: The -jarflag deactivates the standard -cpflag and CLASSPATHenvironment variable, because it retrieves the classpath from the JAR manifest. Any answer that combines -jarand either -cpor $CLASSPATHwill not work.

请注意其他回答java -jar <etc>:该-jar标志停用标准-cp标志和CLASSPATH环境变量,因为它从 JAR 清单中检索类路径。所有的答案,结合-jar,要么-cp$CLASSPATH将无法正常工作。

This information is well-hidden, but I finally found a reference:

这个信息隐藏得很好,但我终于找到了一个参考:

-jar
Execute a program encapsulated in a JAR file. The first argument is the name of a JAR file instead of a startup class name. In order for this option to work, the manifest of the JAR file must contain a line of the form Main-Class: classname. Here, classname identifies the class having the public static void main(String[] args)method that serves as your application's starting point. See the Jar tool reference page and the Jar trail of the Java Tutorial for information about working with Jar files and Jar-file manifests. When you use this option, the JAR file is the source of all user classes, and other user class path settings are ignored.

-jar
执行封装在 JAR 文件中的程序。第一个参数是 JAR 文件的名称,而不是启动类名称。为了使这个选项起作用,JAR 文件的清单必须包含一行 Main-Class: classname 形式。在这里,classname 标识具有public static void main(String[] args)作为应用程序起点的方法的类。有关使用 Jar 文件和 Jar 文件清单的信息,请参阅 Jar 工具参考页面和 Java 教程的 Jar 跟踪。使用此选项时,JAR 文件是所有用户类的来源,其他用户类路径设置将被忽略。

Source:java - the Java application launcher

来源:java - Java 应用程序启动器