我已经尝试了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的,但它们具有类似的概念。


当前回答

我想纠正其他人对NoClassDefFoundError的看法。

NoClassDefFoundError的发生有多种原因,比如:

没有为该引用的类找到.class,无论它在编译时是否可用(i. ClassNotFoundException)。E基类/子类)。 类文件已定位,但在初始化静态变量时引发异常 类文件定位,在初始化静态块时引发异常

在最初的问题中,这是第一个可以通过将CLASSPATH设置为引用的类JAR文件或其包文件夹来纠正的情况。

“在编译时可用”是什么意思?

在代码中使用引用的类。例如:两个类,A和B(扩展A)。如果B在代码中直接引用,则在编译时可用,即A A = new B();

“编译时不可用”是什么意思?

编译时类和运行时类是不同的,例如,基类是使用子类的类名加载的 forname(“名称”) 例如:两个类,A和B(扩展A) A A = Class.forName("B").newInstance();

其他回答

检查类中是否有静态处理程序。如果是这样,请小心,因为静态处理器只能在有循环程序的线程中启动,崩溃可能会以这种方式触发:

首先,在一个简单的线程中创建类的实例并捕获崩溃。 然后在主线程中调用Class的field方法,你会得到NoClassDefFoundError。

下面是测试代码:

public class MyClass{
       private static  Handler mHandler = new Handler();
       public static int num = 0;
}

在Main活动的onCreate方法中,添加测试代码部分:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //test code start
    new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                MyClass myClass = new MyClass();
            } catch (Throwable e) {
                e.printStackTrace();
            }
        }
    }).start();

    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    MyClass.num = 3;
    // end of test code
}

有一个简单的方法来修复它使用一个handlerThread到init处理程序:

private static Handler mHandler;
private static HandlerThread handlerThread = new HandlerThread("newthread");
static {
    handlerThread.start();
    mHandler = new Handler(handlerThread.getLooper(), mHandlerCB);
}

NoClassDefFoundError意味着类在编译时存在于类路径中,但在运行时不存在于类路径中。

如果您正在使用Eclipse,请确保在.classpath文件中将形状、线点和空间对象作为条目。

在我的环境中,我在单元测试中遇到了这个问题。在将一个库依赖项附加到*之后。砰,这是固定的。

例子:

错误信息:

java.lang.NoClassDefFoundError: com/abc/def/foo/xyz/Iottt

POM内容:

<dependency>
    <groupId>com.abc.def</groupId>
    <artifactId>foo</artifactId>
    <scope>test</scope>
</dependency>

如果您正在使用一个以上的模块,您应该有

dexOptions {
    preDexLibraries = false
}

在构建文件中。

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

在编译时类:类是从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).