51 When you deploy a new unified login, you encounter an issue such as:
It is easy to confuse the two errors of Java.lang.NoClassDefFoundError and java.lang.ClassNotfoundException, in fact the two errors are completely different.
Noclassdeffounderror Why the error occurred
The Noclassdeffounderror error occurs because the Java Virtual machine can find the appropriate class at compile time and cannot find the error caused by the appropriate class at run time. For example, when we want to invoke a method of a class or access a static member of the class at run time, we find that the class is not available, and the Java virtual machine throws a noclassdeffounderror error. Unlike classnotfoundexception, this error occurs only when the runtime needs to load the corresponding class unsuccessfully, not at compile time. It's easy for many Java developers to confuse the two errors here.
As a simple summary, noclassdeffounderror occurs when the corresponding class is available at compile time, while the runtime is in Java's classpath path, and the corresponding class is not usable as a result of the error.
Noclassdeffounderror and ClassNotFoundException differences
We are often confused by the two errors of java.lang.ClassNotFoundException and Java.lang.NoClassDefFoundError, although they are all related to Java classpath, but they are completely different. Noclassdeffounderror occurs when the JVM is dynamically running, and the corresponding class is found in the classpath based on the class name you provide, but when it does not find the class, A java.lang.NoClassDefFoundError error occurs, and ClassNotFoundException is the error that occurs when the corresponding class is not found in classpath at compile time. ClassNotFoundException is easier to solve than noclassdeffounderror because we know that errors occur at compile time and are completely caused by environmental problems. And if you work in a Java EE environment and get noclassdeffounderror exceptions, and the corresponding error classes do exist, this means that the class may not be visible to the ClassLoader.
How to resolve Noclassdeffounderror errors
According to the previous article, it is obvious that the Noclassdeffounderror error is because at runtime the ClassLoader cannot find the class that needs to be loaded under classpath, so we need to load the corresponding class into classpath. Or check why the class is not available in classpath, this can happen for the following reasons:
- The corresponding class is not available in Java Classpath
- You may run your program with the jar command, but the class is not defined in the Classpath attribute in the jar file's manifest file
- The startup script of the possible program overrides the original CLASSPATH environment variable
- Because Noclassdeffounderror is a subclass of Java.lang.LinkageError, it may be caused by the unavailability of the native class library that the program relies on.
- Check the log file for errors such as Java.lang.ExceptionInInitializerError, Noclassdeffounderror can be caused by static initialization failure (this is the solution to the problem I am experiencing)
- If you work in a Java EE environment, there are several different classloader that can also cause noclassdeffounderror.
- Noclassdeffounderror can also be caused by a static initialization module error for a class, and when your class performs some static initialization module operations, if the initialization module throws an exception, the other classes that depend on the class will throw noclassdeffounderror errors. If you look at the program log, Will find some java.lang.ExceptionInInitializerError error logs, exceptionininitializererror errors can lead to Java.lang.NoClassDefFoundError : Could not initialize class
about how to resolve java.lang.NoClassDefFoundError errors