如何为两个目录设置 java -classpath?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5781963/
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
How to set java -classpath for two directories?
提问by Jeena
I am trying to get a little test working in Java. I have two directories root
and root/lib
. I have a root/lib/Test.java
file and a root/Foo.java
file. I can compile both with javac lib/Test.java
and then javac -classpath lib Foo.java
and get:
我正在尝试在 Java 中进行一些测试。我有两个目录root
和root/lib
. 我有一个root/lib/Test.java
文件和一个root/Foo.java
文件。我可以同时编译javac lib/Test.java
,然后javac -classpath lib Foo.java
得到:
root/
Foo.java
Foo.class
lib/
Test.java
Test.class
here is how they look like:
这是它们的样子:
class Foo {
static public void main(String[] argv) {
System.out.println(Test.test());
}
}
and
和
class Test {
public static int test() {
return 2;
}
}
How do I run them so they work together without adding a import statement? I have tried java -classpath ".;lib" Foo
but I just get java.lang.NoClassDefFoundError: Foo
如何在不添加导入语句的情况下运行它们以便它们一起工作?我试过了,java -classpath ".;lib" Foo
但我只是得到java.lang.NoClassDefFoundError: Foo
Lala: jeena$ cd root
Lala:root jeena$ java -classpath ".;lib" Foo
Exception in thread "main" java.lang.NoClassDefFoundError: Foo
Caused by: java.lang.ClassNotFoundException: Foo
at java.net.URLClassLoader.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
回答by Isaac Truett
This suggests that you're in the parent of root:
这表明您在 root 的父级中:
javac -classpath root/lib Foo.java
javac -classpath root/lib Foo.java
And this suggests that you're in root:
这表明你在 root 中:
java -classpath ".;lib" Foo
java -classpath ".;lib" Foo
Which directory are you actually working from? Perhaps absolute paths would be easier for you?
你实际上是从哪个目录工作的?也许绝对路径对您来说更容易?
Also, what operating system? On Linux, for example, your path separator will be :
instead of ;
.
还有,什么操作系统?例如,在 Linux 上,您的路径分隔符将:
代替;
.
回答by Joseph Ottinger
I'm not sure what the actual problem is - if you're on windows, and neither class uses a specific package name (i.e., they're in the unnamed package, which you shouldn't use), then "java -classpath .;lib Foo" should be able to load and run Foo without a problem.
我不确定实际问题是什么 - 如果你在 Windows 上,并且两个类都没有使用特定的包名(即,它们在未命名的包中,你不应该使用),然后“java -classpath .;lib Foo" 应该能够毫无问题地加载和运行 Foo。
If they're not in the same package (which you haven't shown) then as long as Test is public, you can access the test() method by using "packageName.goes.here.Test.test()."
如果它们不在同一个包中(您没有显示),那么只要 Test 是公开的,您就可以使用“packageName.goes.here.Test.test()”访问 test() 方法。