When an exception occurs in the static initialization block, the JVM throws a Java.lang.ExceptionInInitializerError exception. If you know the static variables in Java, you will know that they are initialized when the class is loaded. If an exception occurs during initialization of this static variable, a Java.lang.ExceptionInInitializerError exception is thrown. Any exception can cause this, for example, Java.lang.ArrayIndexOutOfBound or java.lang.NullPointerException. Java developers are often stunned by this error and feel that they do not define any static initialization blocks and why they throw exceptionininitializererror exceptions;Java defaults to the initialization of static variables in a default static initialization block, which is then initialized in the order in which they are declared in the source file. For example, if the variable ABC declaration is used in the first row, in the second row, and in the third row, then the second line of code throws a NullPointerException exception, This exception is encapsulated in a Exceptionininitializererror exception, and if the code executes in the main thread, you will see an error message in the console or in the log file: "Exception in Thread" main " Java.lang.ExceptionInInitializerError ". In a large system with a large number of log files, such errors are easily overlooked and the programmer gets a Java.lang.NoClassDefFoundError exception. Unfortunately, this error occurs only when someone else uses the class, because Exceptionininitializererror causes the class to fail to load. Because the class failed to load, the JVM throws Noclassdeffounderror. Sometimes it's misleading for Java developers to check the classpath, path, and Java.library.path to see if they're missing the class, but they can't find any problems, which is confusing. if If you are analyzing noclassdeffounderror reasons, you'd better look at your log file for Exceptionininitializererror, and then consider whether you want to check classpath Exception in thread "main" Reason for Java.lang.ExceptionInInitializerError   As with other errors or exceptions, when you see this line of information, you know that this is a Exceptionininitializererror exception, which is caused by the failure of the static block initialization process during class loading. . Since it appears in the main thread responsible for starting the program, you'd better start parsing from the main class, where the main class is the one you specified in the command-line arguments, or the class that you declared the public static void main (String args[]) method . If you take a closer look at the full stack trace, you don't have to do anything because the JVM has printed the class name, which is the class that triggered the exceptionininitializererror. Exceptionininitializererror is a subclass of Linkageerror, which means that this exception causes your class to fail to load into the JVM's memory . Now let's take a look at this example program, which throws the following exception when it executes: How to resolve exception in thread "main" Java.lang.ExceptionInInitializerError Here are a few things to keep in mind:1. "Exception in Thread" main "java.lang.ExceptionInInitializerError" means that the exception appears in the main thread, And is a subclass of Linkageerror Java.lang.ExceptionInInitializerError, which is thrown when the JVM class fails to load, because there is a static initialization code such as indexoutofboundsexception or null Pointerexception such a runtimeexception. 2. Remember that the JVM places the initialization of all static variables into a static initialization block in the order in which they appear in the source file. Therefore, do not assume that this exception does not occur if you do not see the static initial block. In fact, you have to make sure that the static variables are in the correct order, for example, if a variable is initialized with another variable, you have to make sure that the variable is initialized before. 3. If another code wants to use this class, it throws a Exceptionininitializererror exception, which in turn causes classnotfoundexception or Noclassdeffounderror. Why? Because this class fails to load, it is not loaded into the JVM's memory. So if you're dealing with exceptions such as a class that doesn't exist, let's look at the exception in your log file. 4. Remember that static initialization blocks throw runtimeexception instead of checked exceptions, which require a corresponding catch block for processing. It is necessary to remember that one of the side effects of this anomaly is noclassdeffounderror, The Java program throws this exception in a location that may be far from Java.lang.ExceptionInInitializerError, depending on when your client code refers to this class . Therefore, before looking at the Classpath to resolve noclassdeffounderror exceptions, it is a good idea to check the log for Exceptionininitializererror. The following errors have been examined:
when Java reads the path information in the configuration file, if the path section uses "\" instead of "/", the system defaults to parsing the \u form, so that if there is path information in the configuration file, it should use "\ \" or "/" .
Exceptionininitializererror anomalies in Java and their solutions