简体   繁体   English

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

[英]Java class execution problem: java.lang.ClassNotFoundException

Below is what I tried in linux terminal: compiled Test.java, run Test.class, and got an error. 下面是我在linux终端上尝试的:编译Test.java,运行Test.class,并得到一个错误。 Then, I tried the same command with "-classpath ." 然后,我用“-classpath”尝试了相同的命令。 option and "-cp ." 选项和“-cp”。 option, but also failed. 选项,但也失败了。

/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$ 

But if I remove the package "testpackage" and recompile the source code, the resulting class file is executed well. 但是,如果我删除包“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$

What's wrong with my code, or execution command? 我的代码或执行命令有什么问题? Please help me. 请帮我。 Thank you. 谢谢。 :) :)

You need to run the commands from one directory higher. 您需要从更高的一个目录运行命令。

A class in package foo must live in directory foo . foo的类必须位于目录foo Package foo.bar must be in directory foo/bar and so on. foo.bar必须在目录foo/bar ,依此类推。

So, your structure should have a file called /path/to/code/testpackage/Test.java and your working directory should be /path/to/code . 因此,您的结构应该有一个名为/path/to/code/testpackage/Test.java的文件,您的工作目录应该是/path/to/code You can then run: 然后你可以运行:

javac testpackage/Test.java

java -cp . testpackage.Test

and everything should work. 一切都应该有效。

When you have a package name, the fully resolved class name is testpackage.Test . 如果有包名称,则完全解析的类名称为testpackage.Test That's what java.exe expects to see. 这就是java.exe期望看到的。

You can't work with "testpackage" as your current directory. 您无法使用“testpackage”作为当前目录。 You would need to run it as 你需要运行它

java testpackage.Test

from the directory of which "testpackage" is a subdirectory. 从“testpackage”是子目录的目录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM