What is difference between ClassNotFoundException and NoClassDefFoundError?
can any one able to explain through some example..
What is difference between ClassNotFoundException and NoClassDefFoundError?
can any one able to explain through some example..
Class not found:
http://docs.oracle.com/javase/7/docs/api/java/lang/ClassNotFoundException.html
No Class Def:
http://docs.oracle.com/javase/7/docs/api/java/lang/NoClassDefFoundError.html
"Class not found" is pretty obvious: some reflection mechanism tried to refer to it's class by it's name, but the class wasn't around, so kablooey. That's pretty obvious. The name is wrong.
The sneaky one is the "No Class Def Found" error. That happens when you compile the code & everything is hunky-dorey, but at runtime, that class that was available at compile time just isn't there. Or, even more sneaky, it's there, but at the wrong version so it can't be used.
The first issue points to a configuration error. You've just got the wrong name.
The second issue points to a build error. You've either lost something from the build path, or you're running in a different JVM, or something whacky like that.
"Class Not Found" is usually pretty easy to fix, but "No Class Def" can have you pulling your hair out. If you're experiencing the latter, set about proving that the class mentioned is on the classpath, or in a jar on the classpath. Usually that effort shakes the problem out when I see it.
NoClassDefFoundError
is thrown when a compiled-in "hard" dependency on a class is unsatisfied at runtime. In other words, there's a class that's directly mentioned in your source code, and it was on the classpath at compile time, but it's not on the classpath at runtime.
ClassNotFoundException
is thrown when you try to look up a class dynamically at runtime and it can't be found. In this case the class name wasn't directly referenced in your source code; instead it was specified as a string, which your application may have read from someplace like a configuration file. It's an Exception
instead of an Error
because this situation is one that the application may be able to reasonably recover from — just like the FileNotFoundException
that you get when you try to open a file that doesn't exist.
Both of these can be caused by having something missing from your runtime classpath, but ClassNotFoundException
can also be caused by application-level configuration problems causing your application to try to load classes that it shouldn't.
The major cause of NoClassDefFoundException is a .class file with the wrong name or wrong position in the file system/JAR file for the class it contains or the package statement that was in it when compiled.