Java:如何从命令行导入jar文件 [英] Java: how to import a jar file from command line
问题描述
我试图从命令行(Windows)用Java调用一个类(main方法)。
类导入其他类(其他jar)。
I'm trying to call a class (main method) from command line (Windows) with Java.
The class imports other classes (other jars).
我总是从我的主程序导入的类中得到class not found exception。
I always get "class not found exception" from a class that my main program imports.
这是我试过的:
-
添加CLASSPATH env。 var with所引用的lib所在的路径(不工作)
Add a CLASSPATH env. var with the path where the referenced lib resides (not working)
我尝试使用所有这些不同的参数调用 java -jar从命令行:
-classpath lib /
, -classpath ./lib/命名为myjar.jar
code>, -classpath lib
, -cp lib / *
, -cp lib / \ *
, -classpath lib / referenced-class.jar
, -classpath ./lib/referenced-class.jar
(lib是引用的jar所在的位置)
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)
打包所有引用的jar在我的主类驻留的我的jar里...
I tried packaging all the referenced jar inside my jar where my main class resides...
这样,我还试图在Manifest文件with:
类路径被引用的jar.jar
和我也尝试了类路径lib / referenced-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 tried Class-path lib/referenced-jar.jar
推荐答案
$ c> -jar 命令行参数,如果你碰巧知道要运行的主类的名称:
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:
java -classpath .;myjar.jar;lib/referenced-class.jar my.package.MainClass
如果perchance你使用linux,你应该使用:而不是;在类路径中。
If perchance you are using linux, you should use ":" instead of ";" in the classpath.
这篇关于Java:如何从命令行导入jar文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!