Please help me to understand ClassNotFoundException and NoClassDefFoundError [closed]
Can someone help me to understand ClassNotFoundException
and NoClassDefFoundError
(with a good example for NoClassDefFoundError
)?
NoClassDefFoundError: "Thrown if the Java Virtual Machine or a ClassLoader instance tries to load in the definition of a class (as part of a normal method call or as part of creating a new instance using the new expression) and no definition of the class could be found. " It means it can't find your class (.class file) in the directories it was told to search in for them. So if you start java and run it on some class which includes another class, but you didn't put the class in the directory specified by the -classpath
option when java was started, it will throw this error.
ClassNotFoundException: "Thrown when an application tries to load in a class through its string name using:
The forName method in class Class. The findSystemClass method in class ClassLoader . The loadClass method in class ClassLoader.
but no definition for the class with the specified name could be found. " Since NoClassDefFoundError is thrown by the ClassLoader, It's probably a case of java saying, "where is the .class file you're referring to?"
Example situations when this happens:
1) ClassNotFoundException
Class<?> c = Class.forName("NonExistentClass");
Happens typically with Reflection. For other examples where this can occur, you may refer to the "Use" tab in the Javadocs.
2) NoClassDefFoundError
This means that the class loader requests byte code for a specific class, but can't find it anywhere in the current class path.
Example: Compile an app that uses a second library Jar's code. Once the app is compiled, delete the second library and run the app - you'll get this exception telling you that Java couldn't find the appropriate classes of the second library.
Well you probably already know that it just means the Class that you are looking for was not found by the JVM when trying to compile/run.
Now for the cause - The most common cause of this is a class path not configured properly. If you know a class exists and you are getting an error like this, then recheck your classpath and make sure you include the build directory when running and not the source.
Hope that helps!
The difference between the error and the exception are described by the javadoc.
ClassNotFoundException
"Thrown when an application tries to load in a class through its string name using:
- The forName method in class Class.
- The findSystemClass method in class ClassLoader.
- The loadClass method in class ClassLoader.
but no definition for the class with the specified name could be found."
To get this to happen, just call Class.forName(...)
with the name of a non-existent class.
NoClassDefFoundError
"Thrown if the Java Virtual Machine or a ClassLoader instance tries to load in the definition of a class (as part of a normal method call or as part of creating a new instance using the new expression) and no definition of the class could be found.
The searched-for class definition existed when the currently executing class was compiled, but the definition can no longer be found."
To get this to happen:
- Create an app consisting of an entry point class (with a suitable
main
method), and another class. - Compile both classes.
- Delete the ".class" file for the second one.
- Run the app using
java
with the first class on the classpath.
精彩评论