Spring boot application loads all the beans using bean factory. If any error occurred while invoking the java classes, this exception is thrown



Exception

Exception in thread "restartedMain" java.lang.reflect.InvocationTargetException
     at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
     at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
     at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
     at java.base/java.lang.reflect.Method.invoke(Unknown Source)
     at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)
 Caused by: java.lang.Error: Unresolved compilation problem: 
     SpringApplication cannot be resolved
 at com.yawintutor.SpringBootMySqLApplication.main(SpringBootMySqLApplication.java:11) ... 5 more


Root Cause

Spring boot loads all the java classes using java reflection. The BeanFactory class loads all the beans. If any error occurred while loading a bean in BeanFactory due to reflection, this exception will be thrown.

This error is due to Caused by: java.lang.Error: Unresolved compilation problem It is due to mismatch of java compilation and execution. This error can’t be reproduced.



Solution 1

This issue is due to timing mismatch of java compilation and java execution of a java class. In most of the case, if you restart the application, this issue will be resolved.

If it is not resolved in restart, clean and rebuild the application will resolve this issue.



Solution 2

This issue might occur due to auto configuration. If this issue occurs randomly, then revisit on this dependency.

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>



Leave a Reply