6

NoClassDefFoundError extends LinkageError which in turns extends Error.

Javadoc for Error class states:

An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch.

Class loading methods like Class.forName() only declares ClassNotFoundException in throws clause. Which, in addition to above description of Error means that we should not be usually catching the NoClassDefFoundError when loading classes with Class.forName() etc.

My question is what are the conditions in which NoClassDefFoundError is thrown instead of ClassNotFoundException?

Tahir Akhtar
  • 11,385
  • 7
  • 42
  • 69
  • Exact duplicate of http://stackoverflow.com/questions/1457863/what-is-the-difference-between-noclassdeffounderror-and-classnotfoundexception – skaffman Jul 15 '10 at 10:40
  • I also found a related discussion here: http://stackoverflow.com/questions/430089/in-java-can-class-forname-ever-return-null/430092#430092 – Tahir Akhtar Jul 15 '10 at 10:44

2 Answers2

12

ClassNotFoundException is more likely to be thrown (to your code) in situations where you're manually loading classes - precisely for things like Class.forName(). These names may come from user input, for example.

NoClassDefFoundError will occur when a class file itself refers to a class that then can't be found. The class was present at some time, but now isn't - this isn't just a bug in the code that's trying to do reflection, it's a deployment mistake of not making all the required classes available. As far as I can tell a NoClassDefFoundError will usually or possibly always wrap a ClassNotFoundException - but the point is that this isn't something your code is meant to guard against, as it indicates an environment which is probably too broken to recover from.

At least, that's my understanding :)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 1
    There's another much higher rated question for this, with higher rated answers, but I felt that this answer explained it better and gave information lacking from the others. (Such as the observation that NoClassDefFoundError appears to always wrap ClassNotFoundException, which clears up a lot for me about the intent of these two classes.) – skiphoppy Sep 18 '12 at 14:46
  • @Jon Can you give a practical example of when class names come from user input? – Geek Aug 04 '13 at 15:35
  • @Geek: Well you *could* ask a user to entry a class name, then use `Class.forName` to find that class. – Jon Skeet Aug 04 '13 at 15:56
  • @JonSkeet Is this usual in Java EE applications? I am looking for a more practical example. – Geek Aug 04 '13 at 15:59
  • @Geek: No, it's not very common. It was only *one* source of text data. Another might be an XML configuration file for Dependency Injection, e.g. a Spring config file. – Jon Skeet Aug 04 '13 at 16:00
  • @JonSkeet The Spring example make more sense. Thanks for the clarification and ofcourse +1. – Geek Aug 04 '13 at 16:03
0

NoClassDefFoundError occures at runtime because compiler not able to find .class file.

Nagappa L M
  • 1,452
  • 4
  • 20
  • 33