从命令行运行JAR文件并指定classpath

时间:2021-06-23 13:29:16

I've compiled a JAR file and specified the Main-Class in the manifest (I used the Eclipse Export function). My dependencies are all in a directory labeled lib. I can't seem to get a straight answer on how to execute my JAR file while specifying it should use the lib/* as the classpath.

我编译了一个JAR文件并在清单中指定了Main-Class(我使用了Eclipse Export函数)。我的依赖项都在一个标记为lib的目录中。我似乎无法直接回答如何执行我的JAR文件,同时指定它应该使用lib / *作为类路径。

I've tried:

我试过了:

]$ java -jar -cp .:lib/* MyJar.jar
]$ java -cp .:lib/* -jar MyJar.jar
]$ java -cp .:lib/* com.somepackage.subpackage.Main

etc...

等等...

Each gives an error saying:

每个都给出一个错误说:

Error: Could not find or load main class ....

错误:无法找到或加载主类....

or gives the NoClassDefFoundError indicating the libraries are not being found.

或者给出NoClassDefFoundError,指示未找到库。

I even tried remaking the JAR file and included the lib directory and contents, but still no dice...

我甚至尝试重新制作JAR文件并包含lib目录和内容,但仍然没有骰子......

How can I execute a JAR file from the command line and specify the classpath to use?

如何从命令行执行JAR文件并指定要使用的类路径?

4 个解决方案

#1


139  

When you specify -jar then the -cp parameter will be ignored.

指定-jar时,将忽略-cp参数。

From the documentation:

从文档:

When you use this option, the JAR file is the source of all user classes, and other user class path settings are ignored.

使用此选项时,JAR文件是所有用户类的源,并忽略其他用户类路径设置。

You also cannot "include" needed jar files into another jar file (you would need to extract their contents and put the .class files into your jar file)

您也不能将所需的jar文件“包含”到另一个jar文件中(您需要提取其内容并将.class文件放入您的jar文件中)

You have two options:

你有两个选择:

  1. include all jar files from the lib directory into the manifest (you can use relative paths there)
  2. 将lib目录中的所有jar文件包含到清单中(可以在那里使用相对路径)
  3. Specify everything (including your jar) on the commandline using -cp:
    java -cp MyJar.jar:lib/* com.somepackage.subpackage.Main
  4. 使用-cp指定命令行上的所有内容(包括jar):java -cp MyJar.jar:lib / * com.somepackage.subpackage.Main

#2


26  

Run a jar file and specify a class path like this:

运行jar文件并指定类路径,如下所示:

java -cp <jar_name.jar:libs/*> com.test.App

jar_name.jar is the full name of the JAR you want to execute

jar_name.jar是您要执行的JAR的全名

libs/* is a path to your dependency JARs

libs / *是依赖JAR的路径

com.test.App is the fully qualified name of the class from the JAR that has the main(String[]) method

com.test.App是JAR中具有main(String [])方法的类的完全限定名

The jar and dependent jar should have execute permissions.

jar和依赖jar应具有执行权限。

#3


1  

You can do these in unix shell:

您可以在unix shell中执行以下操作:

java -cp MyJar.jar:lib/* com.somepackage.subpackage.Main

You can do these in windows powershell:

您可以在Windows PowerShell中执行以下操作:

java -cp "MyJar.jar;lib\*" com.somepackage.subpackage.Main

#4


0  

Alternatively, use the manifest to specify the class-path and main-class if you like, so then you don't need to use -cp or specify the main class. In your case it would contain lines like this:

或者,如果您愿意,可以使用清单指定类路径和主类,因此您不需要使用-cp或指定主类。在你的情况下,它将包含这样的行:

Main-Class: com.test.App
Class-Path: lib/one.jar lib/two.jar

Unfortunately you need to spell out each jar in the manifest (not a biggie as you only do once, and you can use a script to build the file or use a build tool like ANT or Maven or Gradle). And the reference has to be a relative or absolute directory to where you run the java -jar MyJar.jar.

不幸的是,你需要拼出清单中的每个jar(不像你只做一次,你可以使用脚本来构建文件或使用像ANT或Maven或Gradle这样的构建工具)。并且引用必须是运行java -jar MyJar.jar的相对或绝对目录。

Then execute it with

然后执行它

java -jar MyJar.jar

#1


139  

When you specify -jar then the -cp parameter will be ignored.

指定-jar时,将忽略-cp参数。

From the documentation:

从文档:

When you use this option, the JAR file is the source of all user classes, and other user class path settings are ignored.

使用此选项时,JAR文件是所有用户类的源,并忽略其他用户类路径设置。

You also cannot "include" needed jar files into another jar file (you would need to extract their contents and put the .class files into your jar file)

您也不能将所需的jar文件“包含”到另一个jar文件中(您需要提取其内容并将.class文件放入您的jar文件中)

You have two options:

你有两个选择:

  1. include all jar files from the lib directory into the manifest (you can use relative paths there)
  2. 将lib目录中的所有jar文件包含到清单中(可以在那里使用相对路径)
  3. Specify everything (including your jar) on the commandline using -cp:
    java -cp MyJar.jar:lib/* com.somepackage.subpackage.Main
  4. 使用-cp指定命令行上的所有内容(包括jar):java -cp MyJar.jar:lib / * com.somepackage.subpackage.Main

#2


26  

Run a jar file and specify a class path like this:

运行jar文件并指定类路径,如下所示:

java -cp <jar_name.jar:libs/*> com.test.App

jar_name.jar is the full name of the JAR you want to execute

jar_name.jar是您要执行的JAR的全名

libs/* is a path to your dependency JARs

libs / *是依赖JAR的路径

com.test.App is the fully qualified name of the class from the JAR that has the main(String[]) method

com.test.App是JAR中具有main(String [])方法的类的完全限定名

The jar and dependent jar should have execute permissions.

jar和依赖jar应具有执行权限。

#3


1  

You can do these in unix shell:

您可以在unix shell中执行以下操作:

java -cp MyJar.jar:lib/* com.somepackage.subpackage.Main

You can do these in windows powershell:

您可以在Windows PowerShell中执行以下操作:

java -cp "MyJar.jar;lib\*" com.somepackage.subpackage.Main

#4


0  

Alternatively, use the manifest to specify the class-path and main-class if you like, so then you don't need to use -cp or specify the main class. In your case it would contain lines like this:

或者,如果您愿意,可以使用清单指定类路径和主类,因此您不需要使用-cp或指定主类。在你的情况下,它将包含这样的行:

Main-Class: com.test.App
Class-Path: lib/one.jar lib/two.jar

Unfortunately you need to spell out each jar in the manifest (not a biggie as you only do once, and you can use a script to build the file or use a build tool like ANT or Maven or Gradle). And the reference has to be a relative or absolute directory to where you run the java -jar MyJar.jar.

不幸的是,你需要拼出清单中的每个jar(不像你只做一次,你可以使用脚本来构建文件或使用像ANT或Maven或Gradle这样的构建工具)。并且引用必须是运行java -jar MyJar.jar的相对或绝对目录。

Then execute it with

然后执行它

java -jar MyJar.jar