我已经尝试了Oracle的Java教程中的两个示例。它们都可以编译,但在运行时,都会出现这个错误:

Exception in thread "main" java.lang.NoClassDefFoundError: graphics/shapes/Square
    at Main.main(Main.java:7)
Caused by: java.lang.ClassNotFoundException: graphics.shapes.Square
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 1 more

我想我可能把Main.java文件放在错误的文件夹里了。

下面是目录层次结构:

graphics
├ Main.java
├ shapes
|   ├ Square.java
|   ├ Triangle.java
├ linepoint
|   ├ Line.java
|   ├ Point.java
├ spaceobjects
|   ├ Cube.java
|   ├ RectPrism.java

这是Main.java:

import graphics.shapes.*;
import graphics.linepoint.*
import graphics.spaceobjects.*;

public class Main {
    public static void main(String args[]) {
        Square s = new Square(2, 3, 15);
        Line l = new Line(1, 5, 2, 3);
        Cube c = new Cube(13, 32, 22);
    }
}

我哪里做错了?

更新

在我把Main类放入图形包之后(我添加了图形包;对于它),将类路径设置为“_test”(包含图形的文件夹),编译它,并使用Java图形运行它。Main(从命令行),它工作正常。

真的很晚更新#2

我没有使用Eclipse(只有notepad++和JDK),上面的更新解决了我的问题。然而,这些答案中似乎有许多是针对Eclipse和IntelliJ IDEA的,但它们具有类似的概念。


当前回答

我使用Eclipse的FileSync插件,所以我可以在Tomcat上进行实时调试。我收到了NoClassFoundError,因为我在Tomcat的元数据中为Eclipse workspace =>类中的bin目录添加了一个同步条目,但我还没有为Eclipse =>中的extlib目录添加一个文件夹同步

C:\Users\Stuart\eclipse-workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\webapps\myApp\WEB-INF\lib

其他回答

我的观点是:

确保类路径包含完整路径(/home/user/lib/some_lib.jar而不是~/lib/some_lib.jar),否则仍然会面临NoClassDefFoundError错误。

这种情况经常发生在我的Genymotion设备上。

确保在安装Genymotion的驱动器上有足够的可用内存。

在一个NetBeans项目上工作了好几个月之后,在收到“低内存”警报后不久,我突然收到了NoClassDefFoundError消息。进行Clean重建并没有帮助,但是完全关闭NetBeans并重新启动项目时没有出现错误报告。

当在类路径中找不到预期的类时,就会发生无类定义异常。

在编译时类:类是从Java编译器生成的,但在运行时不知道为什么找不到依赖类。

让我们来看一个简单的例子:

public class ClassA{
    public static void main(String args[]){
         // Some gibberish code...
         String text = ClassB.getString();
         System.out.println("Text is: " + text);
    }
}

public class ClassB{
    public static String getString(){
        return "Testing some exception";
    }
}

现在让我们假设上述两个Java源代码被放置在某个文件夹中,例如“NoClassDefinationFoundExceptionDemo”

现在打开一个shell(假设Java已经正确设置)

Go to folder "NoClassDefinationFoundExceptionDemo" Compile Java source files javac ClassB javac ClassA Both files are compiled successfully and generated class files in the same folder as ClassA.class and ClassB.class Now since we are overriding ClassPath to the current working director, we execute the following command java -cp . ClassA and it worked successfully and you will see the output on the screen Now let's say, you removed ClassB.class file from the present directory. And now you execute the command again. java -cp . ClassA Now it will greet you with NoClassDefFoundException. As ClassB which is a dependency for ClassA is not found in the classpath (i.e., the present working directory).

如果你的项目是在一个包,如com。blahcode和你的类被称为Main,编译后的文件可能会以。/out/com/blahcode/Main.class这样的目录结构输出。对于IntelliJ IDEA来说尤其如此。

当试图从shell或cmd运行时,您需要cd到包含com作为子目录的目录。

cd out
java -classpath . com.blahcode.Main