大学 bash 中的 Java 因 NoClassDefFoundError 失败

发布于 2024-08-06 14:07:42 字数 2122 浏览 9 评论 0原文

我知道有很多关于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

月依秋水 2024-08-13 14:07:42

首先,使用 javac hello.java 生成 hello.class 文件。然后,使用命令 java 执行该类 hello - 使用 java 命令时不包含 .class 扩展名。

另外,我想指出,类名以大写字母开头是惯例 - hello 应该是 Hello

First, compile your .java file using the javac hello.java to produce a hello.class file. Then, execute the class using the command java hello - you don't include the .class extension when using the java command.

Also, I would like to point out that it is convention that class names begin with a capital letter - hello should be Hello.

勿挽旧人 2024-08-13 14:07:42

我认为你真的先编译了这个类吗?:

javac hello.java
java hello

I take it you did you actually compile the class first?:

javac hello.java
java hello
水晶透心 2024-08-13 14:07:42
java -verbose hello.class

... 表示“嘿,Java,运行类 'hello.class' 中的 main() 方法。Java

找不到名为“hello.class”的类。您的类名为“hello”。

java -verbose hello

由于 '.'在你的类路径中,Java 会在 './hello.class' 中找到 'hello' 类。

额外提示:Java 中通常以大写字母开头,

public class Hello {

这有助于区分其余的类引用和变量引用。你的代码。

Dessert dessert= new Dessert("tiramisu")
java -verbose hello.class

... 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".

java -verbose 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.

public class Hello {

This helps to distinguish between class references and variable references in the rest of your code.

Dessert dessert= new Dessert("tiramisu")
左秋 2024-08-13 14:07:42
java -verbose hello
java -verbose hello
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文