大学 bash 中的 Java 因 NoClassDefFoundError 失败
我知道有很多关于 NoClassDefFoundError 的帖子,它们似乎都在谈论 jar 文件。虽然我对 Eclipse 中的 java 很满意,但我很困惑为什么我能想到的最简单的东西不起作用,除非他们在大学方面破坏了一些东西。
public class hello {
public static void main (String args[]) {
System.out.println ("Hello World!");
}
}
这是整个 hello.java 程序,它抛出:
Exception in thread "main" java.lang.NoClassDefFoundError: hello/java
Caused by: java.lang.ClassNotFoundException: hello.java
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
Could not find the main class: hello.java. Program will exit.
$CLASSPATH=./:/usr/java/latest/lib:/home/41/myusername/bin
java -verbose hello.class
给出:
[Opened /usr/java/jdk1.6.0_10/jre/lib/rt.jar] [Loaded java.lang.Object from /usr/java/jdk1.6.0_10/jre/lib/rt.jar] [Loaded java.io.Serializable from /usr/java/jdk1.6.0_10/jre/lib/rt.jar] [Loaded java.lang.Comparable from /usr/java/jdk1.6.0_10/jre/lib/rt.jar] [Loaded java.lang.CharSequence from /usr/java/jdk1.6.0_10/jre/lib/rt.jar] [Loaded java.lang.String from /usr/java/jdk1.6.0_10/jre/lib/rt.jar] [Loaded java.lang.reflect.GenericDeclaration from /usr/java/jdk1.6.0_10/jre/lib/rt.jar] [Loaded java.lang.reflect.Type from /usr/java/jdk1.6.0_10/jre/lib/rt.jar] [Loaded java.lang.reflect.AnnotatedElement from /usr/java/jdk1.6.0_10/jre/lib/rt.jar] [Loaded java.lang.Class from /usr/java/jdk1.6.0_10/jre/lib/rt.jar] ... [Loaded sun.misc.AtomicLong from /usr/java/jdk1.6.0_10/jre/lib/rt.jar] ...
再次出现异常。
java -version
给出:
java version "1.6.0_10-rc2" Java(TM) SE Runtime Environment (build 1.6.0_10-rc2-b32) Java HotSpot(TM) Server VM (build 11.0-b15, mixed mode)
I know there are many posts concerning NoClassDefFoundError, they all seem to be talking about jar files. While I'm comfortable with java in eclipse, I'm pretty lost as to why the simplest thing I can come up with is not functioning, unless they broke something on the university side of this.
public class hello {
public static void main (String args[]) {
System.out.println ("Hello World!");
}
}
This is the entire hello.java program which throws this:
Exception in thread "main" java.lang.NoClassDefFoundError: hello/java
Caused by: java.lang.ClassNotFoundException: hello.java
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
Could not find the main class: hello.java. Program will exit.
$CLASSPATH=./:/usr/java/latest/lib:/home/41/myusername/bin
java -verbose hello.class
gives:
[Opened /usr/java/jdk1.6.0_10/jre/lib/rt.jar] [Loaded java.lang.Object from /usr/java/jdk1.6.0_10/jre/lib/rt.jar] [Loaded java.io.Serializable from /usr/java/jdk1.6.0_10/jre/lib/rt.jar] [Loaded java.lang.Comparable from /usr/java/jdk1.6.0_10/jre/lib/rt.jar] [Loaded java.lang.CharSequence from /usr/java/jdk1.6.0_10/jre/lib/rt.jar] [Loaded java.lang.String from /usr/java/jdk1.6.0_10/jre/lib/rt.jar] [Loaded java.lang.reflect.GenericDeclaration from /usr/java/jdk1.6.0_10/jre/lib/rt.jar] [Loaded java.lang.reflect.Type from /usr/java/jdk1.6.0_10/jre/lib/rt.jar] [Loaded java.lang.reflect.AnnotatedElement from /usr/java/jdk1.6.0_10/jre/lib/rt.jar] [Loaded java.lang.Class from /usr/java/jdk1.6.0_10/jre/lib/rt.jar] ... [Loaded sun.misc.AtomicLong from /usr/java/jdk1.6.0_10/jre/lib/rt.jar] ...
with the exception again.
java -version
gives:
java version "1.6.0_10-rc2" Java(TM) SE Runtime Environment (build 1.6.0_10-rc2-b32) Java HotSpot(TM) Server VM (build 11.0-b15, mixed mode)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
首先,使用
javac
hello.java
生成hello.class
文件。然后,使用命令java 执行该类
hello
- 使用java
命令时不包含.class
扩展名。另外,我想指出,类名以大写字母开头是惯例 -
hello
应该是Hello
。First, compile your
.java
file using thejavac
hello.java
to produce ahello.class
file. Then, execute the class using the commandjava
hello
- you don't include the.class
extension when using thejava
command.Also, I would like to point out that it is convention that class names begin with a capital letter -
hello
should beHello
.我认为你真的先编译了这个类吗?:
I take it you did you actually compile the class first?:
... 表示“嘿,Java,运行类 'hello.class' 中的 main() 方法。Java
找不到名为“hello.class”的类。您的类名为“hello”。
由于 '.'在你的类路径中,Java 会在 './hello.class' 中找到 'hello' 类。
额外提示:Java 中通常以大写字母开头,
这有助于区分其余的类引用和变量引用。你的代码。
... means "hey, Java, run the main() method in the class 'hello.class'.
Java can't find a class named "hello.class". Your class is called "hello".
Since '.' is in your classpath, Java will find the 'hello' class in './hello.class'.
Extra tip: it's conventional in Java to start classes with a capital letter.
This helps to distinguish between class references and variable references in the rest of your code.