How to solve java.lang.NoClassDefFoundError?

前端 未结 27 1494
暗喜
暗喜 2020-11-21 06:04

I\'ve tried both the example in Oracle\'s Java Tutorials. They both compile fine, but at run-time, both come up with this error:

Exception in thread \"main\"         


        
27条回答
  •  清酒与你
    2020-11-21 06:35

    NoClassDefFoundError in Java:

    Definition:

    NoClassDefFoundError will come if a class was present during compile time but not available in java classpath during runtime. Normally you will see below line in log when you get NoClassDefFoundError: Exception in thread "main" java.lang.NoClassDefFoundError

    Possible Causes:

    1. The class is not available in Java Classpath.

    2. You might be running your program using jar command and class was not defined in manifest file's ClassPath attribute.

    3. Any start-up script is overriding Classpath environment variable.

    4. Because NoClassDefFoundError is a subclass of java.lang.LinkageError it can also come if one of it dependency like native library may not available.

    5. Check for java.lang.ExceptionInInitializerError in your log file. NoClassDefFoundError due to the failure of static initialization is quite common.

    6. 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.

    Possible Resolutions:

    1. Verify that all required Java classes are included in the application’s classpath. The most common mistake is not to include all the necessary classes, before starting to execute a Java application that has dependencies on some external libraries.

    2. The classpath of the application is correct, but the Classpath environment variable is overridden before the application’s execution.

    3. Verify that the aforementioned ExceptionInInitializerError does not appear in the stack trace of your application.

    Resources:

    3 ways to solve java.lang.NoClassDefFoundError in Java J2EE

    java.lang.NoClassDefFoundError – How to solve No Class Def Found Error

提交回复
热议问题