Java类执行问题:java.lang.ClassNotFoundException

前端之家收集整理的这篇文章主要介绍了Java类执行问题:java.lang.ClassNotFoundException前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
下面是我在 linux终端中试过的:编译Test. java,运行Test.class,并得到一个错误.然后,我尝试使用“-classpath”相同的命令.选项和“-cp”.选项,但也失败.
/testpackage$cat Test.java 
package testpackage;

public class Test {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println("May I take your order?");
    }

}
/testpackage$javac Test.java 
/testpackage$java testpackage.Test
Exception in thread "main" java.lang.NoClassDefFoundError: testpackage/Test
Caused by: java.lang.ClassNotFoundException: testpackage.Test
    at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
Could not find the main class: testpackage.Test. Program will exit.

/testpackage$java -cp . testpackage.Test
Exception in thread "main" java.lang.NoClassDefFoundError: testpackage/Test
Caused by: java.lang.ClassNotFoundException: testpackage.Test
    at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
Could not find the main class: testpackage.Test. Program will exit.

/testpackage$java -classpath . testpackage.Test
Exception in thread "main" java.lang.NoClassDefFoundError: testpackage/Test
Caused by: java.lang.ClassNotFoundException: testpackage.Test
    at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
Could not find the main class: testpackage.Test. Program will exit.
/testpackage$

但是如果我删除包“testpackage”并重新编译源代码,则生成的类文件执行得很好.

/testpackage$cat Test.java
//package testpackage;

public class Test {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println("May I take your order?");
    }

}
/testpackage$javac Test.java
/testpackage$java Test
May I take your order?
/testpackage$

我的代码或执行命令有什么问题?请帮帮我.
谢谢.

猜你在找的Java相关文章