This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds and have James Denton on-line!
See this thread for details.
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Differnce between NoClassDefFoundError and ClassNotFoundException  RSS feed

 
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!