.jar 库的 Java 命令行问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/3056277/
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 command-line problems with .jar libraries
提问by Monster
I have a single .java (driver.java) file I'm trying to compile and run from the command-line. It uses the external library called EXT.jar, whose structure is just a folder called EXT with a few dozen classes within it.
我有一个 .java (driver.java) 文件,我正在尝试从命令行编译和运行。它使用名为 的外部库EXT.jar,其结构只是一个名为 EXT 的文件夹,其中包含几十个类。
So I run:
所以我运行:
javac -cp EXT.jar driver.java
This compiles the class just fine.
这很好地编译了类。
then when I run:
然后当我运行时:
java -cp EXT.jar driver
I get a java.lang.NoClassDefFoundError.
我得到一个java.lang.NoClassDefFoundError.
Oddly enough, if I unpack the JAR (so now I have a folder in the root directory called EXT), the last command works just fine!! Driver will execute!
奇怪的是,如果我解压 JAR(所以现在我在根目录中有一个名为 EXT 的文件夹),最后一个命令工作正常!!驱动程序将执行!
Is there any way I can make the driver.class look for the need class files from EXT.jar/EXT/*classinstead of an actual EXT folder?
有什么办法可以让 driver.class 从而EXT.jar/EXT/*class不是实际的 EXT 文件夹中查找需要的类文件吗?
Thanks!
谢谢!
回答by Matt
You're compiling the class to the local directory. So when you run it, you need to include the current directory in your classpath. E.g.:
您正在将类编译到本地目录。所以当你运行它时,你需要在你的类路径中包含当前目录。例如:
java -cp .;EXT.jar driver
Or in linux:
或者在 linux 中:
java -cp .:EXT.jar driver
With the way you have it now, you're saying your classpath is onlyEXT.jar (along with whatever is in the CLASSPATH environment variable) and nothing else (which is why the current directory, where driver.class is located, is excluded)
以您现在拥有的方式,您是说您的类路径只是EXT.jar(以及 CLASSPATH 环境变量中的任何内容)而没有其他内容(这就是为什么 driver.class 所在的当前目录被排除在外的原因) )

