[Exception resolution] java.lang.NoClassDefFoundError: Could not initialize class com.iot.alarm.ProcAlar occurred during Java runtime

1. Background description

Springboot + JDK1.8, the program is compiled normally, and an exception is thrown after calling a certain class at runtime.
There is no abnormal program at compile time, and an exception is thrown at runtime called NoClassDefFoundError: Could not initialize class class name . The detailed error information is shown in the figure below:
insert image description here

2. Reason Analysis

According to the official Java documentation, NoClassDefFoundError is an exception thrown when the JVM or ClassLoader instance tries to load the definition of a class in order to call a method of a class or a new instance of a new class, but cannot find its definition. It should be noted that for the case where this exception is thrown, the definition of the class you are trying to find exists at compile time, but it is missing at runtime.
insert image description hereNoClassDefFoundError generally has two situations, the class file does not exist, or the class initialization error. And their error messages are also different, we can distinguish them according to the displayed information.

  • If the class file is initialized incorrectly, the error message is NoClassDefFoundError: Could not initialize class class name as mentioned above . This is usually caused by a static member of a class or a static initializer block . For example, private static final ApplicationEventPublisher eventPublisher = SpringUtil.getBean(ApplicationEventPublisher.class); or a statement directly executed in the static {} code block throws an exception, which may cause NoClassDefFoundError . For the first case, you can add a try{} catch(){} statement in the static initializer of the class to catch the exception and output the log to understand the specific error content.
  • If the class file cannot be found, the error message is java.lang.NoClassDefFoundError: com/example/class .

3. Solutions

For the above two different error contents, we list the following solutions for your convenience:

3.1 Initialization error resolution

If you don’t know which class has an error, then we add try{} catch() {}, a statement block to the problematic code, and re-run the program, the error message will print in detail which class has a problem, For example, the following code:
insert image description hereFor example, the detailed error message after restarting
insert image description herecan be seen from the figure because there is no ApplicationEventPublisher instance available, then we can find out the code related to it from the class and modify it. For me, I commented out this code first, so it doesn't affect the business, or just write a replacement solution.
insert image description here

3.2 Unable to find class file solution

It may be due to the lack of dependencies, add dependencies and recompile and then re-verify. Or if the class is missing, just add the missing class.

This article is over!

Guess you like

Origin blog.csdn.net/weixin_44299027/article/details/130396186