San Domingo's Blog

Courage, Duty, Honor!

Class Not Found Exception and Error

| Comments

java.lang.ClassNotFoundException:

Java Doc

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.

This exception indicates that the class was not found on the classpath. This indicates that we were trying to load the class definition, and the class did not exist on the classpath.

该异常一般在运行时想要动态加载一个类时,发现该类不在classpath上时会被抛出。

java.lang.NoClassDefFoundError:

Java Doc

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.

This exception indicates that the JVM looked in its internal class definition data structure for the definition of a class and did not find it. This is different than saying that it could not be loaded from the classpath. Usually this indicates that we previously attempted to load a class from the classpath, but it failed for some reason – now we’re trying to use the class again (and thus need to load it, since it failed last time), but we’re not even going to try to load it, because we failed loading it earlier (and reasonably suspect that we would fail again). The earlier failure could be a ClassNotFoundException or an ExceptionInInitializerError (indicating a failure in the static initialization block) or any number of other problems. The point is, a NoClassDefFoundError is not necessarily a classpath problem.

简而言之就是类在加载的时候发生了错误,可能是

  • 类的静态初始化(块/域)出了问题

  • 编译成功的.class文件未出现在classpath中

« keep going Macbook修复万金油 »

Comments