Experiment Scenario 1:
- The entire compilation generates the target directory and.class files
- Delete com. Wangfan. Sign. Consts. Const. Class files
- Start the AoeTest. Main
public class Const {
public static final Integer NM = 222222;
}
public class AoeTest {
public static void main(String[] args) {
try {
System.out.println(Const.NM);
} catch(Throwable e) { e.printStackTrace(); }}}Copy the code
Output result:
java.lang.NoClassDefFoundError: com/wangfan/consts/Const
at com.wfan.goldenTolwer.AoeTest.main(AoeTest.java:10)
Caused by: java.lang.ClassNotFoundException: com.wangfan.consts.Const
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)...1 more
Copy the code
Conclusion:
- NoClassDefFoundError is caused by ClassNotFoundException
- ClassNotFoundException because the runtime, through this when loading such find com. Wangfan. Sign. Consts. Const. Class files.
Experiment Scenario 2:
- The entire compilation generates the target directory and.class files
- Write Const as follows
- Start the AoeTest. Main
public class Const {
public static final Integer SB = Integer.valueOf(null);
}
public class AoeTest {
public static void main(String[] args) {
try {
System.out.println(Const.SB);
} catch(Throwable e) { e.printStackTrace(); } System.out.println(Const.SB); }}Copy the code
Abnormal results:
java.lang.ExceptionInInitializerError
at com.wfan.goldenTolwer.AoeTest.main(AoeTest.java:10)
Caused by: java.lang.NumberFormatException: null
at java.lang.Integer.parseInt(Integer.java:542)
at java.lang.Integer.valueOf(Integer.java:766)
at com.wangfan.consts.Const.<clinit>(Const.java:7)...1 more
Exception in thread "main" java.lang.NoClassDefFoundError: Could not initialize class com.wangfan.consts.Const
at com.wfan.goldenTolwer.AoeTest.main(AoeTest.java14) :Copy the code
Conclusion:
- * * System. For the first time out. Println (Const. SB) * * because when initializing Const, static variables is unusual, ExceptionInInitializerError, so initialization failed.
- A NoClassDefFoundError is raised the second time system.out.println (conconst.SB)** is initialized, making the Const class unavailable.
Conclusion:
- ClassNotFoundException can occur at compile time or at run time.
- Occurs at compile time, usually due to a compilation error, or a CLASspath error.
- Occur at runtime, when is this dynamic class loading can’t find the com. Wangfan. Sign. Consts. Const. Class files. Such as URLClassLoader. FindClass
protectedClass<? > findClass(final String name)
throws ClassNotFoundException
{
finalClass<? > result;try {
result = AccessController.doPrivileged(
newPrivilegedExceptionAction<Class<? > > () {publicClass<? > run()throws ClassNotFoundException {
String path = name.replace('. '.'/').concat(".class");
Resource res = ucp.getResource(path, false);
if(res ! =null) {
try {
return defineClass(name, res);
} catch (IOException e) {
throw newClassNotFoundException(name, e); }}else {
return null;
}
}
}, acc);
} catch (java.security.PrivilegedActionException pae) {
throw (ClassNotFoundException) pae.getException();
}
if (result == null) {
throw new ClassNotFoundException(name);
}
return result;
}
Copy the code
- NoClassDefFoundError can be loaded into a. Class file, but when the class is initialized, an exception occurs in a static variable or a static block, causing the loaded class to become unavailable.
reference
www.baeldung.com/java-classn… Dzone.com/articles/ja… My.oschina.net/jasonultima…
- ClassNotFoundException
ClassNotFoundException is a checked exception which occurs when an application tries to load a class through its fully-qualified name and can not find its definition on the classpath. This occurs mainly when trying to load classes using Class.forName(), ClassLoader.loadClass() or ClassLoader.findSystemClass(). Therefore, we need to be extra careful of java.lang.ClassNotFoundException while working with reflection.
- NoClassDefFoundError
NoClassDefFoundError is a fatal error. It occurs when JVM can not find the definition of the class while trying to: Instantiate a class by using the new keyword Load a class with a method call The error occurs when a compiler could successfully compile the class, but Java runtime could not locate the class file. It usually happens when there is an exception while executing a static block or initializing static fields of the class, so class initialization fails. Let’s consider a scenario which is one simple way to reproduce the issue. ClassWithInitErrors initialization throws an exception. So, when we try to create an object of ClassWithInitErrors, it throws ExceptionInInitializerError.