How to resolve java.lang.reflect.InvocationTargetException when running springboot applications
Problem
In this post, I will demonstrate how to resolve the following error when running Spring Boot applications:
Caused by: java.lang.reflect.InvocationTargetException ... 1024 moreCaused by: java.lang.reflect.InvocationTargetException ... 1024 moreCaused by: java.lang.reflect.InvocationTargetException ... 1024 moreCaused by: java.lang.reflect.InvocationTargetException ... 1024 more
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:
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion>
<groupId>com.bswen.test</groupId> <artifactId>shopper</artifactId> <version>1.0-SNAPSHOT</version>
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.5.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent>
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties>
<dependencies> <!-- springboot web and jsp and tomcat embeded --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <executions> <execution> <goals> <goal>repackage</goal> </goals> <configuration> <classifier>exec</classifier> </configuration> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build></project>
Run the code and get error
When I run the Spring Boot app after building it like this:
java -jar shopper-1.0-SNAPSHOT-exec.jar
I get this error:
Caused by: java.lang.reflect.InvocationTargetException ... 1024 moreCaused by: java.lang.reflect.InvocationTargetException ... 1024 moreCaused by: java.lang.reflect.InvocationTargetException ... 1024 moreCaused by: java.lang.reflect.InvocationTargetException ... 1024 more
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:
➜ target git:(master) ✗ cat META-INF/MANIFEST.MFManifest-Version: 1.0Implementation-Title: shopperImplementation-Version: 1.0-SNAPSHOTStart-Class: org.springframework.boot.loader.JarLauncherSpring-Boot-Classes: BOOT-INF/classes/Spring-Boot-Lib: BOOT-INF/lib/Build-Jdk-Spec: 1.8Spring-Boot-Version: 2.1.5.RELEASECreated-By: Maven Archiver 3.4.0Main-Class: org.springframework.boot.loader.JarLauncher
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:
<properties> <!-- The main class to start by executing "java -jar" --> <start-class>com.bswen.test.shopper.Application</start-class></properties>
Alternatively, change the Maven build plugin configuration:
<plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <mainClass>com.bswen.test.shopper.Application</mainClass> </configuration> <executions> <execution> <goals> <goal>repackage</goal> </goals> <configuration> <classifier>exec</classifier> </configuration> </execution> </executions></plugin>
After rebuilding, the META-INF/MANIFEST.MF
in the executable JAR will look like this:
➜ target git:(master) cat META-INF/MANIFEST.MFManifest-Version: 1.0Implementation-Title: shopperImplementation-Version: 1.0-SNAPSHOTStart-Class: com.bswen.test.shopper.Application #this is the keypointSpring-Boot-Classes: BOOT-INF/classes/Spring-Boot-Lib: BOOT-INF/lib/Build-Jdk-Spec: 1.8Spring-Boot-Version: 2.1.5.RELEASECreated-By: Maven Archiver 3.4.0Main-Class: org.springframework.boot.loader.JarLauncher
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:
Oh, and if you found these resources useful, don’t forget to support me by starring the repo on GitHub!