当我运行Java应用程序时,我得到了一个NoClassDefFoundError。造成这种情况的典型原因是什么?


当前回答

当我没有在项目的Java Build Path中的“Order and export”选项卡上导出一个类时,我得到了NoClassDefFound错误。确保在添加到项目构建路径的任何依赖项的“Order and Export”选项卡中添加一个复选标记。请参见Eclipse警告:XXXXXXXXXXX.jar将不会被导出或发布。可能会导致运行时ClassNotFoundExceptions。

其他回答

下面的技巧帮助了我很多次:

System.out.println(TheNoDefFoundClass.class.getProtectionDomain().getCodeSource().getLocation());

其中nodeffoundclass是可能会“丢失”的类,这是由于您的程序使用相同库的旧版本的首选项。当客户端软件被部署到一个占主导地位的容器中,并配备了自己的类加载器和大量最流行的库的古老版本时,这种情况最常发生。

有一个有趣的例子,你可能会看到很多NoClassDefFoundErrors:

在类的静态块中抛出一个RuntimeException 拦截它(或者如果它在测试用例中抛出并不重要) 尝试创建该类的实例

static class Example {
    static {
        thisThrowsRuntimeException();
    }
}

static class OuterClazz {

    OuterClazz() {
        try {
            new Example();
        } catch (Throwable ignored) { //simulating catching RuntimeException from static block
            // DO NOT DO THIS IN PRODUCTION CODE, THIS IS JUST AN EXAMPLE in StackOverflow
        }

        new Example(); //this throws NoClassDefFoundError
    }
}

NoClassDefError将伴随来自静态块RuntimeException的ExceptionInInitializerError一起抛出。


当你在UNIT TESTS中看到NoClassDefFoundErrors时,这一点尤其重要。

在某种程度上,您在测试之间“共享”静态块执行,但初始的ExceptionInInitializerError将仅在一个测试用例中。第一个使用有问题的Example类。其他使用Example类的测试用例只会抛出NoClassDefFoundErrors。

这是迄今为止我找到的最好的解决办法。

假设我们有一个名为org的包。Mypackage包含的类:

HelloWorld(主类) SupportClass UtilClass

定义这个包的文件物理存储在目录D:\myprogram (Windows)或/home/user/myprogram (Linux)下。

文件结构如下所示:

当调用Java时,我们指定要运行的应用程序的名称:org.mypackage.HelloWorld。但是,我们还必须告诉Java在哪里查找定义包的文件和目录。所以要启动程序,我们必须使用下面的命令:

更新[https://www.infoq.com/articles/single-file-execution-java11/]:

在Java SE 11中,可以选择启动单个源代码文件 直接,不需要中间编译。为了方便大家, 所以像你这样的新手不需要运行javac + Java(当然, 让他们感到困惑)。

Java中的NoClassDefFoundError

定义:

Java虚拟机无法在运行时找到在编译时可用的特定类。 如果一个类在编译时存在,但在运行时在java类路径中不可用。

例子:

The class is not in Classpath, there is no sure shot way of knowing it but many times you can just have a look to print System.getproperty("java.classpath") and it will print the classpath from there you can at least get an idea of your actual runtime classpath. A simple example of NoClassDefFoundError is class belongs to a missing JAR file or JAR was not added into classpath or sometimes jar's name has been changed by someone like in my case one of my colleagues has changed tibco.jar into tibco_v3.jar and the program is failing with java.lang.NoClassDefFoundError and I were wondering what's wrong. Just try to run with explicitly -classpath option with the classpath you think will work and if it's working then it's a sure short sign that someone is overriding java classpath. Permission issue on JAR file can also cause NoClassDefFoundError in Java. Typo on XML Configuration can also cause NoClassDefFoundError in Java. when your compiled class which is defined in a package, doesn’t present in the same package while loading like in the case of JApplet it will throw NoClassDefFoundError in Java.

可能的解决方式:

The class is not available in Java Classpath. If you are working in J2EE environment than the visibility of Class among multiple Classloader can also cause java.lang.NoClassDefFoundError, see examples and scenario section for detailed discussion. Check for java.lang.ExceptionInInitializerError in your log file. NoClassDefFoundError due to the failure of static initialization is quite common. Because NoClassDefFoundError is a subclass of java.lang.LinkageError it can also come if one of it dependency like native library may not available. Any start-up script is overriding Classpath environment variable. You might be running your program using jar command and class was not defined in manifest file's ClassPath attribute.

资源:

3种方法解决NoClassDefFoundError

noclassdeffounderror问题模式