File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Differnce between NoClassDefFoundError and ClassNotFoundException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Differnce between NoClassDefFoundError and ClassNotFoundException" Watch "Differnce between NoClassDefFoundError and ClassNotFoundException" New topic
Author

Differnce between NoClassDefFoundError and ClassNotFoundException

Sajee Joseph
Ranch Hand

Joined: Jan 17, 2001
Posts: 200
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
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1873
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
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1873
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Differnce between NoClassDefFoundError and ClassNotFoundException