The same

  • In both cases, the class was not found and an exception was thrown

ClassNotFoundException

  • Belong to the Exception,
  • If the Class is not found in the classpath, a ClassNotFoundException is thrown when the application is running and tries to load a Class file using the classloader
  • . Usually, when we use the Class class.forname () or this. LoadClass and use this. The findSystemClass () Class is loaded at run time, if the Class has not been found, This will cause the JVM to throw a ClassNotFoundException.

NoClassDefFoundError

  • If it is an error, it is usually an error that belongs to the JVM itself and should not be caught
  • When the JVM loads a class, it raises a NoClassDefFoundError error if the class was available at compile time but the definition of the class cannot be found at run time (such as when the.class file is deleted)
public class TempClass {}public class MainClass {
    public static void main(String[] args) {
        TempClass t = newTempClass(); }}Copy the code
public class TempClass {}public class MainClass {
    public static void main(String[] args) {
        TempClass t = newTempClass(); }}Copy the code
Exception in thread "main" java.lang.NoClassDefFoundError: TempClass
    at MainClass.main(MainClass.java:6)
Caused by: java.lang.ClassNotFoundException: TempClass
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)...1 more
Copy the code