Java:如何从命令行导入 jar 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/945962/
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
Java: how to import a jar file from command line
提问by
I'm trying to call a class (main method) from command line (Windows) with Java.
The class imports other classes (other jars).
我正在尝试使用 Java 从命令行(Windows)调用一个类(主方法)。
该类导入其他类(其他 jars)。
I always get "class not found exception" from a class that my main program imports.
我总是从我的主程序导入的类中得到“找不到类的异常”。
Here's what I tried:
这是我尝试过的:
Add a CLASSPATH env. var with the path where the referenced lib resides (not working)
I tried with all these different parameters when calling "
java -jar myjar.jar" from command line : "-classpath lib/", "-classpath ./lib/", "-classpath lib", "-cp lib/*", "-cp lib/\*", "-classpath lib/referenced-class.jar", "-classpath ./lib/referenced-class.jar" (lib is where the referenced jar resides)I tried packaging all the referenced jar inside my jar where my main class resides...
And with all that, I also tried to specify the classes inside the Manifest file with:
Class-path referenced-jar.jarand I also triedClass-path lib/referenced-jar.jar
添加一个 CLASSPATH 环境。var 与引用的 lib 所在的路径(不工作)
java -jar myjar.jar从命令行调用“ ”时,我尝试了所有这些不同的参数:“-classpath lib/”、“-classpath ./lib/”、“-classpath lib”、“-cp lib/*”、“-cp lib/\*”、“-classpath lib/referenced-class.jar”、“-classpath ./lib/referenced-class.jar”(lib 是引用的 jar 所在的位置)我尝试将所有引用的 jar 打包到我的主类所在的 jar 中...
尽管如此,我还尝试使用以下命令指定清单文件中的类:
Class-path referenced-jar.jar并且我也尝试过Class-path lib/referenced-jar.jar
采纳答案by Adam Paynter
You could run it without the -jarcommand line argument if you happen to know the name of the main class you wish to run:
-jar如果您碰巧知道要运行的主类的名称,则可以在没有命令行参数的情况下运行它:
java -classpath .;myjar.jar;lib/referenced-class.jar my.package.MainClass
If perchance you are using linux, you should use ":" instead of ";" in the classpath.
如果您使用的是 linux,则应该使用“:”而不是“;” 在类路径中。
回答by Michael Myers
If you're running a jar file with java -jar, the -classpathargument is ignored. You need to set the classpath in the manifest file of your jar, like so:
如果您使用 运行 jar 文件java -jar,则忽略该-classpath参数。您需要在 jar 的清单文件中设置类路径,如下所示:
Class-Path: jar1-name jar2-name directory-name/jar3-name
Class-Path: jar1-name jar2-name directory-name/jar3-name
See the Java tutorials: Adding Classes to the JAR File's Classpath.
请参阅 Java 教程:将类添加到 JAR 文件的类路径。
Edit:I see you already tried setting the class path in the manifest, but are you sure you used the correct syntax? If you skip the ':' after "Class-Path" like you showed, it would not work.
编辑:我看到您已经尝试在清单中设置类路径,但是您确定使用了正确的语法吗?如果你像你展示的那样跳过 " :" 之后的 ' ' Class-Path,它就行不通。
回答by cupakob
you can try to export as "Runnable jar" in eclipse. I have also problems, when i export as "jar", but i have never problems when i export as "Runnable jar".
您可以尝试在 Eclipse 中导出为“Runnable jar”。我也有问题,当我导出为“jar”时,但当我导出为“Runnable jar”时我从来没有问题。
回答by artemb
try
尝试
java -cp "your_jar.jar:lib/referenced_jar.jar" com.your.main.Main
If you are on windows, you should use ;instead of :
如果你在 Windows 上,你应该使用;而不是:

