How to solve java.lang.NoClassDefFoundError?

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

    This answer is specific to a java.lang.NoClassDefFoundError happening in a service:

    My team recently saw this error after upgrading an rpm that supplied a service. The rpm and the software inside of it had been built with Maven, so it seemed that we had a compile time dependency that had just not gotten included in the rpm.

    However, when investigating, the class that was not found was in the same module as several of the classes in the stack trace. Furthermore, this was not a module that had only been recently added to the build. These facts indicated it might not be a Maven dependency issue.

    The eventual solution: Restart the service!

    It appears that the rpm upgrade invalidated the service's file handle on the underlying jar file. The service then saw a class that had not been loaded into memory, searched for it among its list of jar file handles, and failed to find it because the file handle that it could load the class from had been invalidated. Restarting the service forced it to reload all of its file handles, which then allowed it to load that class that had not been found in memory right after the rpm upgrade.

    Hope that specific case helps someone.

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

    I had the same issue with my Android development using Android studio. Solutions provided are general and did not help me ( at least for me). After hours of research I found following solution and may help to android developers who are doing development using android studio. modify the setting as below Preferences ->Build, Execution, Deployment -> Instant Run -> un-check the first option.

    With this change I am up and running. Hope this will help my dev friends.

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

    One source of error for this exception could stem from inconsistent definitions for Proguard, e.g. a missing

    -libraryJars "path.to.a.missing.jar.library".

    This explains why compilation and running works fine, given that the jar is there, while clean & build fails. Remember to define newly added jar libraries in proguard setup!

    Note that error messages from Proguard are really not up to standard, as they are easily confused with similar ant messages arriving when the jar is not there at all. Only at the very bottom will there be a small hint of proguard in trouble. Hence, it is quite logic to start searching for traditional classpath errors etc, but this will be in vain.

    Evidently, the NoClassDefFound exception will be the results when running e.g. the resulting executable jar built and based on a lack of proguard consistency. Some call it proguard "Hell"

    0 讨论(0)
  • if you recently added multidex support in android studio like this:

    // To Support MultiDex
    implementation 'com.android.support:multidex:1.0.1'
    

    so your solution is just extend From MultiDexApplication instead of Application

    public class MyApp extends MultiDexApplication {
    
    0 讨论(0)
  • 2020-11-21 06:27

    If you are using more than one module, you should have

    dexOptions {
        preDexLibraries = false
    }
    

    in your build file.

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

    It happens a lot with my genymotion devices. Make sure you have a good amount of memory available on your drive where Genymotion is installed.

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