I'm trying to call a class (main method) from command line (Windows) with Java.
The class imports other classes (other jars).
我试图用Java从命令行(Windows)调用一个类(主方法)。该类导入其他类(其他jar)。
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)
添加一个类路径env。使用被引用的lib所在的路径(不工作)
-
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)我在调用“java -jar myjar”时尝试了所有这些不同的参数。jar"来自命令行:"-classpath lib/", "-classpath lib ./lib/", "-classpath lib/*", "-cp lib/\*", "-classpath lib/referenced-class。jar”、“类路径。/ lib / referenced-class。jar (lib是引用的jar所在的地方)
-
I tried packaging all the referenced jar inside my jar where my main class resides...
我尝试将所有引用的jar打包到我的主类所在的jar中……
-
And with all that, I also tried to specify the classes inside the Manifest file with:
Class-path referenced-jar.jar
and I also triedClass-path lib/referenced-jar.jar
尽管如此,我还尝试使用:Class-path referenced-jar来指定清单文件中的类。jar和我还尝试了Class-path lib/referenced-jar.jar。
4 个解决方案
#1
116
You could run it without the -jar
command 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,您应该在类路径中使用“:”而不是“;”。
#2
59
If you're running a jar file with java -jar
, the -classpath
argument is ignored. You need to set the classpath in the manifest file of your jar, like so:
如果使用java -jar运行jar文件,则忽略-classpath参数。您需要在jar的manifest文件中设置类路径,如下所示:
Class-Path: jar1-name jar2-name directory-name/jar3-name
- path:jar1-name jar2-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.
编辑:我看到您已经尝试在清单中设置类路径,但是您确定您使用了正确的语法吗?如果你跳过“类路径”之后的“:”,它就不会工作。
#3
13
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上,你应该使用;而不是:
#4
3
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”时,我从来没有问题。
#1
116
You could run it without the -jar
command 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,您应该在类路径中使用“:”而不是“;”。
#2
59
If you're running a jar file with java -jar
, the -classpath
argument is ignored. You need to set the classpath in the manifest file of your jar, like so:
如果使用java -jar运行jar文件,则忽略-classpath参数。您需要在jar的manifest文件中设置类路径,如下所示:
Class-Path: jar1-name jar2-name directory-name/jar3-name
- path:jar1-name jar2-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.
编辑:我看到您已经尝试在清单中设置类路径,但是您确定您使用了正确的语法吗?如果你跳过“类路径”之后的“:”,它就不会工作。
#3
13
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上,你应该使用;而不是:
#4
3
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”时,我从来没有问题。