Java 从命令行运行 JAR 文件并指定类路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18413014/
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 a JAR file from the command line and specify classpath
提问by SnakeDoc
I've compiled a JARfile and specified the Main-Class in the manifest (I used the Eclipse Exportfunction). 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导出功能)。我的依赖项都在一个标记为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 ....
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 文件并指定要使用的类路径?
采纳答案by a_horse_with_no_name
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:
您有两个选择:
- include all jar files from the
lib
directory into the manifest (you can use relative paths there) - Specify everything (includingyour jar) on the commandline using
-cp
:java -cp MyJar.jar:lib/* com.somepackage.subpackage.Main
- 将
lib
目录中的所有 jar 文件包含到清单中(您可以在那里使用相对路径) - 使用以下命令在命令行上指定所有内容(包括您的 jar)
-cp
:java -cp MyJar.jar:lib/* com.somepackage.subpackage.Main
回答by Sid
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
是来自具有该main(String[])
方法的 JAR 的类的完全限定名称
The jar and dependent jar should have execute permissions.
jar 和从属 jar 应具有执行权限。
回答by tars
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
回答by HankCa
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
回答by Tim de Vries
You can do a Runtime.getRuntime.exec(command) to relaunch the jar including classpath with args.
您可以执行 Runtime.getRuntime.exec(command) 以重新启动包含带有 args 的类路径的 jar。