java.lang.NoClassDefFoundError: Could not initialize class XXX

后端 未结 10 1183
無奈伤痛
無奈伤痛 2020-11-27 10:59
public class PropHolder {
  public static Properties prop;

  static {
    //code for loading properties from file
  }
}

// Referencing the class somewhere else:
Pr         


        
相关标签:
10条回答
  • 2020-11-27 11:22

    I had the same exception, this is how I solved the problem:

    Preconditions:

    1. Junit class (and test), that extended another class.

    2. ApplicationContext initialized using spring, that init the project.

    3. The Application context was initialized in @Before method

    Solution:

    Init the application context from @BeforeClass method, since the parent class also required some classes that were initialized from within the application context.

    Hope this will help.

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

    I had the same exception - but only while running in debug mode, this is how I solved the problem (after 3 whole days): in the build.gradle i had : "multiDexEnabled true" set in the defaultConfig section.

            defaultConfig {
        applicationId "com.xxx.yyy"
        minSdkVersion 15
        targetSdkVersion 28
        versionCode 5123
        versionName "5123"
        // Enabling multidex support.
        multiDexEnabled true
    }
    

    but apparently this wasn't enough. but when i changed:

    public class MyAppClass  extends Application 
    

    to:

    public class MyAppClass  extends MultiDexApplication 
    

    this solved it. hope this will help someone

    0 讨论(0)
  • 2020-11-27 11:29

    If you're working on an Android project, make sure you aren't calling any static methods on any Android classes. I'm only using JUnit + Mockito, so maybe some other frameworks might help you avoid the problem altogether, I'm not sure.

    My problem was calling Uri.parse(uriString) as part of a static initializer for a unit test. The Uri class is an Android API, which is why the unit test build couldn't find it. I changed this value to null instead and everything went back to normal.

    0 讨论(0)
  • 2020-11-27 11:34

    As mentioned above, this could be a number of things. In my case I had a statically initialized variable which relied on a missing entry in my properties file. Added the missing entry to the properties file and the problem was solved.

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