[英]NoClassDeffFoundError when running java.exe from different directory than .class files
I'm trying to learn to run java apps from windows command line and I can't figure out one problem. 我正在尝试从Windows命令行学习运行Java应用程序,但我找不到一个问题。
I have a simple class on my desktop: 我的桌面上有一个简单的类:
public class Hello{
public static void main(String[] args){
System.out.println("1, two, three");
}
}
If I run javac
and java
commands when I'm in my desktop directory in cmd everything is well, but if I go one directory back (so I won't be in the same directory as the .java and .class files) then my cmd directory is C:\\Users\\Tomas
and my Hello.java and Hello.class files are in C:\\Users\\Tomas\\Desktop
. 如果我在cmd的桌面目录中运行
javac
和java
命令时一切正常,但是如果我返回一个目录(因此我将不会与.java和.class文件位于同一目录中), cmd目录为C:\\Users\\Tomas
,我的Hello.java和Hello.class文件位于C:\\Users\\Tomas\\Desktop
。 I can run the command javac Desktop\\Hello.java
and it works, but then if I try to do java Desktop\\Hello.java
I get an Exception in thread "main" java.lang.NoClassDefFoundError: Desktop\\Hello (wrong name: Hello)
. 我可以运行命令
javac Desktop\\Hello.java
并且可以运行,但是如果我尝试执行java Desktop\\Hello.java
则会Exception in thread "main" java.lang.NoClassDefFoundError: Desktop\\Hello (wrong name: Hello)
出现Exception in thread "main" java.lang.NoClassDefFoundError: Desktop\\Hello (wrong name: Hello)
I know that NoClassDefFoundError is throws when a class was available at compile time, but ClassLoader can't find it during run time ( found a good article about it here ). 我知道当在编译时有一个类可用时,会引发NoClassDefFoundError,但是ClassLoader在运行时找不到它(在此处找到了一篇不错的文章)。 I think the problem has something to do with the CLASSPATH variable, so I set it to:
我认为问题与CLASSPATH变量有关,因此将其设置为:
"C:\Program Files\Java\jdk1.7.0_21\jre\lib\ext";.;"C:\Program Files\Java\jdk1.7.0_21\jre\bin";"C:\Users\Tomas\Desktop"
(I included "C:\\Users\\Tomas\\Desktop" just to try everything) And I tried running the "java" command with -classpath and -cp options: (我包括“ C:\\ Users \\ Tomas \\ Desktop”只是为了尝试所有操作),并且尝试使用-classpath和-cp选项运行“ java”命令:
java -classpath "C:\Program Files\Java\jdk1.7.0_21\jre\lib\ext";.;"C:\Program Files\Java\jdk1.7.0_21\jre\bin";"C:\Users\Tomas\Desktop" Desktop\Hello
And I keep getting the same exception. 而且,我不断收到同样的例外。 I't would be great if someone can explain my error and why this is happening, and maybe point even give some directions where can I read more about this.
如果有人可以解释我的错误以及发生这种错误的原因,并且甚至给出一些指导,让我可以在此了解更多信息,我就不会很棒。
Thank you. 谢谢。
class 类
package Desktop;
public class Hello{
public static void main(String[] args){
System.out.println("1, two, three");
}
}
compile (here Desktop
means standart windows directory) 编译(此处的
Desktop
表示标准的Windows目录)
javac Desktop\Hello.java
execute (here Desktop
means package
. Desktop/Hello
is fully class name) 执行(此处
Desktop
表示package
Desktop/Hello
是完整的类名)
java Desktop/Hello
java -classpath 'C:\Users\Tomas\Desktop\Hello.class'
应该运行它。
Try java -classpath "C:\\Program Files\\Java\\jdk1.7.0_21\\jre\\lib\\ext";.;"C:\\Program Files\\Java\\jdk1.7.0_21\\jre\\bin";"C:\\Users\\Tomas\\Desktop" Hello 尝试java -classpath“ C:\\ Program Files \\ Java \\ jdk1.7.0_21 \\ jre \\ lib \\ ext”;。;“” C:\\ Program Files \\ Java \\ jdk1.7.0_21 \\ jre \\ bin“;” C:\\ Users \\ Tomas \\ Desktop“您好
I only removed Desktop from your class name. 我只是从您的班级名称中删除了Desktop。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.