This week's book giveaway is in the Jython/Python forum.
We're giving away four copies of Hands On Software Engineering with Python and have Brian Allbey on-line!
See this thread for details.
Win a copy of Hands On Software Engineering with Python this week in the Jython/Python 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

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
 
He's dead Jim. Grab his tricorder. I'll get his wallet and this tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!