How to solve java.lang.NoClassDefFoundError?

前端 未结 27 1536
暗喜
暗喜 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:30

    Check that if you have a static handler in your class. If so, please be careful, cause static handler only could be initiated in thread which has a looper, the crash could be triggered in this way:

    1.firstly, create the instance of class in a simple thread and catch the crash.

    2.then call the field method of Class in main thread, you will get the NoClassDefFoundError.

    here is the test code:

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

    in your onCrete method of Main activity, add test code part:

    @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
    }
    

    there is a simple way to fix it using a handlerThread to init handler:

    private static Handler mHandler;
    private static HandlerThread handlerThread = new HandlerThread("newthread");
    static {
        handlerThread.start();
        mHandler = new Handler(handlerThread.getLooper(), mHandlerCB);
    }
    
    0 讨论(0)
  • 2020-11-21 06:33

    Don't use test classes outside the module

    I do not have a solution, just another flavour of the "present at compilation, absent at run time" case.

    I was trying to use a very convenient method from a JUnit test class from another test class which resides in a different module. That's a no-no, since test code is not part of the packaged jar, but I didn't realize because it appears visible for the user class from within Eclipse.

    My solution was to place the method in a existing utilities class that is part of the production code.

    0 讨论(0)
  • 2020-11-21 06:34

    I'd like to correct the perspective of others on NoClassDefFoundError.

    NoClassDefFoundError can occur for multiple reasons like

    1. ClassNotFoundException -- .class not found for that referenced class irrespective of whether it is available at compile time or not(i.e base/child class).
    2. Class file located, but Exception raised while initializing static variables
    3. Class file located, Exception raised while initializing static blocks

    In the original question, it was the first case which can be corrected by setting CLASSPATH to the referenced classes jar file or to its package folder.

    What it means by saying "available in compile time"?

    • The referenced class is used in the code.
      Eg: Two classes, A and B(extends A). If B is referenced directly in the code, it is available at compile time, i.e. A a = new B();

    What it means by saying "not available at compile time"?

    • The compile time class and runtime class are different, i.e. for example base class is loaded using classname of child class for example Class.forName("classname")
      Eg: Two classes, A and B(extends A). Code has
      A a = Class.forName("B").newInstance();
    0 讨论(0)
  • 2020-11-21 06:34

    I use the FileSync plugin for Eclipse so I can live debug on Tomcat & I received NoClassFoundError because I had added a sync entry for the bin directory in the Eclipse workspace => classes in the metadata for Tomcat but hadn't also added a folder sync for the extlib directory in Eclipse =>

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

    0 讨论(0)
  • 2020-11-21 06:34

    I'm developing an Eclipse based application also known as RCP (Rich Client Platform). And I have been facing this problem after refactoring (moving one class from an plugIn to a new one).

    Cleaning the project and Maven update didn't help.

    The problem was caused by the Bundle-Activator which haven't been updated automatically. Manual update of the Bundle-Activator under MANIFEST.MF in the new PlugIn has fixed my problem.

    0 讨论(0)
  • 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

    0 讨论(0)
提交回复
热议问题