java-How to solve java.lang.reflect.InvocationTargetException when running springboot applications

Problem

In this post, I would demo how to solve the problem as follows when running springboot applications:

Caused by: java.lang.reflect.InvocationTargetException
        ... 1024 more
Caused by: java.lang.reflect.InvocationTargetException
        ... 1024 more
Caused by: java.lang.reflect.InvocationTargetException
        ... 1024 more
Caused 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 springboot app after build like this:

java -jar shopper-1.0-SNAPSHOT-exec.jar

I get this error:

Caused by: java.lang.reflect.InvocationTargetException
        ... 1024 more
Caused by: java.lang.reflect.InvocationTargetException
        ... 1024 more
Caused by: java.lang.reflect.InvocationTargetException
        ... 1024 more
Caused by: java.lang.reflect.InvocationTargetException
        ... 1024 more

How to solve the problem

I think this error is caused by the missing of main-class in the jar:

Let’s inspect the jar by unzip it:

And the content of META-INF/MANIFEST.MF is:

➜  target git:(master) ✗ cat META-INF/MANIFEST.MF
Manifest-Version: 1.0
Implementation-Title: shopper
Implementation-Version: 1.0-SNAPSHOT
Start-Class: org.springframework.boot.loader.JarLauncher
Spring-Boot-Classes: BOOT-INF/classes/
Spring-Boot-Lib: BOOT-INF/lib/
Build-Jdk-Spec: 1.8
Spring-Boot-Version: 2.1.5.RELEASE
Created-By: Maven Archiver 3.4.0
Main-Class: org.springframework.boot.loader.JarLauncher

In fact, spring boot expect that there is a main-class to be set to org.springframework.boot.loader.JarLauncherin the manifest.mf, and a start-class to be set to the springboot Application class.

So the problem is the the start-class param in the manifest.mf, which is wrongly set , it should be the spring boot application’s main class.

Fix it like this: add this property to your maven pom or gradle

<properties>
      <!-- The main class to start by executing "java -jar" -->
      <start-class>com.bswen.test.shopper.Application</start-class>
</properties>

or change the maven build plugin params:

            <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 build, we get this in the META-INF/MANIFEST.MF in the exec jar:

➜  target git:(master) cat META-INF/MANIFEST.MF 
Manifest-Version: 1.0
Implementation-Title: shopper
Implementation-Version: 1.0-SNAPSHOT
Start-Class: com.bswen.test.shopper.Application  #this is the keypoint
Spring-Boot-Classes: BOOT-INF/classes/
Spring-Boot-Lib: BOOT-INF/lib/
Build-Jdk-Spec: 1.8
Spring-Boot-Version: 2.1.5.RELEASE
Created-By: Maven Archiver 3.4.0
Main-Class: org.springframework.boot.loader.JarLauncher

You can see that, after change the pom, you get a correct start-class in the manifest.mf

References

You can view some references as follows: