Forums Register Login

Differnce between NoClassDefFoundError and ClassNotFoundException

+Pie Number of slices to send: Send
Hello all,

What is the difference between NoClassDefFoundError and ClassNotFoundException. I know one is an Error and other is an exception. Can someone explain in terms of an example?

Regards,
Saj
+Pie Number of slices to send: Send
Hi sajee

ClassNotFound is thrown when a system can't find a class its trying to load. e.g.
Class.forName("someClass"); and if system can't find the class with "someClass" name it will throw this exception.

example,
Create a class D which tries to load class named "someClass" which doesn't exist,


Here is the output when you try to run the class D,


NoClassDefFound is thrown when the system can't find a dependent class which was there when the containing class got compiled. e.g.
Create two classes A.java , B.java like,



Now compile A class like,
$javac A.java

It will get compiled and you will see A.class and B.class created.

You can run class A like,
$java A

This will work fine.

Now rename the B.class file with something else, say C.class.

Now run class A again,
$java A

It will give this error,



Hope this helps.

Similar google groups link,
http://groups.google.com/group/comp.lang.java.programmer/browse_thread/thread/c8c15027576869f/dad6446f897f8946?lnk=st&q=classnotfoundexception+noclassdeffounderror&rnum=1#dad6446f897f8946

Regards,
Maulin
+Pie Number of slices to send: Send
The reason one is Exception and the other is Error is,

When a system can't find a class that needs to be loaded then its an exceptional situation which the caller of the program (client) might want to handle in some way ...

BUT when a system can't find a class which was there originally on compilation time (otherwise we would get symbol not resolved error on compile time for class B in our example above), THEN its an Error. Hence it throws NoClassDefFoundError.

Regards
Maulin
Evil is afoot. But this tiny ad is just an ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 2643 times.
Similar Threads
ClassNotFoundException Vs NoClassDefFoundError
NoClassDefFoundError
NoClassDefFoundError
NoClassDefFoundError
NoClassDefFoundError
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 15, 2024 23:54:20.