Tuesday, October 6, 2009

Is there any difference between java.lang.NoClassDefFoundError and java.lang.ClassNotFoundException ?

When we started learning java most of time we may come across java.lang.ClassNotFoundException when we try to run some simple programs. Later we found that this error was throwing due to the class was not in the jvm class-path.
java.lang.NoClassDefFoundError will be thrown when the class was loaded and it cannot be initialized properly. As JVM can find the classes in the class-path and it cannot create a Class object of the class which is loaded.
Below are few scenarios where the class initializing may throw exception.
·        When a exception was thrown inside the static block of the class.
o   The reason is the static block is executed when the Class object is getting loaded, if any error occurs it can't be initialized.
·        Any Exception was thrown when initialing a static variable of the class.
o   The reason remains same as static block. It also getting executed during Class load.

Then the morel of the story is when the class was not properly initialized then we will get java.lang.NoClassDefFoundError, mainly due the static blocks and static variables.
Simple Example which will throw java.lang.NoClassDefFoundError..

public class AnotherClass {
          static String test = "new";
          static AnotherClass staticObject = new AnotherClass();

          public AnotherClass() {
                   String a = null;
                   a.length();
          }
}

public class SimpleTest {
          public static void main(String[] args) {
                   AnotherClass a= null;
                   try {
                             a = AnotherClass.staticObject;
                   } catch (Error e) {
                             e.printStackTrace();
                   }
                   System.out.println(AnotherClass.test);
          }
}
When SimpleTest calles the "AnotherClass.staticObject" the class get loaded and the static variable staticObject calles the constructor where the initialization failes… below is the error trace

java.lang.ExceptionInInitializerError
          at passionsoft.bloggerspot.com.SimpleTest.main(SimpleTest.java:11)
Caused by: java.lang.NullPointerException
          at passionsoft.bloggerspot.com.AnotherClass.<init>(AnotherClass.java:12)
          at passionsoft.bloggerspot.com.AnotherClass.<clinit>(AnotherClass.java:7)
          ... 1 more
Exception in thread "main" java.lang.NoClassDefFoundError
          at passionsoft.bloggerspot.com.SimpleTest.main(SimpleTest.java:19)

As per my understanding I can think only two scenarios when the java.lang.NoClassDefFoundError was thrown if you guys know some more scenarios please update the comments.:)

2 comments:

Nicky said...

Hi,

Thanks for the article!

The typical cause of the NoClassDefFoundError is: The class being loaded was available during compilation but is now gone during runtime.

Example.
1) Say you have the following two classes:
A.java => class A { }
B.java => class B { }

2) And say that A contains a "main" method which makes a "new B();".

3) Compile these and you get: A.class and B.class.

4) Now if you do a "java A[enter]" then everything is fine.

5) Remove B.class and try it again ... Then you get the ClassDefNotFound error!

:)

Keep up the good work!

Carlos said...

I wonder why this exception was given this misleading name, instead of ClassInitializationException or something like that? It could even have the original exception as a root cause.

Post a Comment

 

This content comes from a hidden element on this page.

The inline option preserves bound JavaScript events and changes, and it puts the content back where it came from when it is closed.
Click me, it will be preserved!

If you try to open a new ColorBox while it is already open, it will update itself with the new content.

Updating Content Example:
Click here to load new content