In this post, I will demonstrate how to resolve the following error when running Spring Boot applications:
Environments
The environments are as follows:
Java version: JDK 1.8
Spring Boot version: 2.1.5.RELEASE
The Maven POM
The code is as follows:
Run the code and get error
When I run the Spring Boot app after building it like this:
I get this error:
How to resolve the problem
I think this error is caused by the missing main class in the JAR. Let’s inspect the JAR by unzipping it:
The content of META-INF/MANIFEST.MF is:
In fact, Spring Boot expects that there is a main-class to be set to org.springframework.boot.loader.JarLauncher in the MANIFEST.MF, and a start-class to be set to the Spring Boot application’s main class.
To fix this, add the following property to your Maven POM:
Alternatively, change the Maven build plugin configuration:
After rebuilding, the META-INF/MANIFEST.MF in the executable JAR will look like this:
You can see that, after changing the POM, you get a correct start-class in the MANIFEST.MF.
Summary
In this post, we explored how to resolve the java.lang.reflect.InvocationTargetException error in Spring Boot applications. The key issue was the incorrect configuration of the start-class parameter in the MANIFEST.MF file. By correctly setting the start-class to the main class of the Spring Boot application, either through the Maven POM properties or the Spring Boot Maven plugin configuration, the issue can be resolved. Always ensure that your MANIFEST.MF file is correctly configured to avoid such runtime errors.
Final Words + More Resources
My intention with this article was to help others who might be considering solving such a problem.
So I hope that’s been the case here. If you still have any questions, don’t hesitate to ask me by
email: Email me
Here are also the most important links from this article along with some further resources that will help you in this scope: