-1

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?

11
  • 5
    Dont obfuscate the code, became hard to understand Commented Jun 19, 2013 at 7:10
  • Please post your code here.. Commented Jun 19, 2013 at 7:14
  • please provide more details to explain your problem - showing some real code will rise the possibility of answers Commented Jun 19, 2013 at 7:14
  • how did you build your jar? is class D your own code or an external dependency? Commented Jun 19, 2013 at 7:14
  • put your code.... then only we can understand what actually you want to say. or put an image of the same concept... Commented Jun 19, 2013 at 7:15

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

"Another pb could come from the D class initialization which failed" <-- if this were the case there would be another exception prior to this NoClassDefFoundError
it depends. I think that static block initialization raised a no class def found error if something goes wrong because the JVM is not able to load the class definition
1

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.