简体   繁体   中英

java.lang.NoClassDefFoundError with static final method

I have a class having static final methods.[Say A,B,C].

C invokes another class D[D's package is imported in C].

The maven assembly jar[say M.jar] that I have DOESN'T HAVE package D. During runtime, when I try to call A having M.jar in classpath, getting noclasdef error saying D is not present.

Why I am getting this?

package TEST1
import test.CHECK.TestA;
import test.CHECK.TestB;
class Factory

{

final static A()
{
//some ref to test.CHECK.TestA
}


 static B()
{
//some ref to test.CHECK.TestB
}

static  C()
{
}

I have jar containing this class and package test.CHECK.TestB in that jar. However, this jar doesn't contain test.CHECK.TestA.

Now, my client program having this jar calls C() . Then, getting ClassNotFoundException for TestA, though we are not calling A(). Why is this so?

No class def found error means that your class was found but the JVM failed to load it during runtime. Most of the time the problem is your class D is not loaded in the same classloader as the class calling it. Another problem could come from the D class initialization which failed due to some obscure reasons... We need you to provide the complete stacktrace if you want some help.

Why you're getting ClassNotFoundException :

When the JVM loads a class in memory, it tries to load its dependencies as well (other classes this class' code depends on). As your class has references to test.CHECK.TestA, the JVM tries to load also this missing class (see The Java Virtual Machine Specification, chapter 12, also this: ClassLoader : possible configure to use lazy and not static resolution? ).

How to make the JVM not to try to load the missing class? :

That's not possible at least with Oracle's JVM

Possible solution :

Try searching in Google/Bing/Whatever the whole name of the missing class (including package name) to see if you can find the missing class and add it to your classpath

Another possible solution (extremely ugly workaround -- children, don't do this at home) :

If you can't find such class and if you are desperate to make this run, AND you are TOTALLY SURE method A is never used, even by method C which is the one you use, then try by "mocking" (write "empty" substitutes) the missing package, the missing class and the missing methods; this would involve a try-error process of compiling/executing/error_thrown/edit_again. I know this is utterly ugly, but at the end of the day you could get your class run.

I think the problem is when you are calling C() static method, your code will be referencing Factory class for the first time (assuming its not loaded in memory).

So when your Factory class is loaded, it will try to load all the static methods and at that time JVM is not able to load TestA class due to some reason, that's why that error comes off.

Try calling A() method from the class that calls C() to see if it succeeds.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM