简体   繁体   中英

ClassNotFoundException when running fat JAR configured by spring-boot-maven-plugin

I am trying to create executable JAR file for my spring boot application. To achieve this spring-boot-maven-plugin has been used with main class specified and packaging to jar. Unfortunately after running output JAR file i receive java.lang.ClassNotFoundException pointing to my main class.

Below is my pom.xml and main class. Thanks in advance for help.

pom.xml:

    <?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.test</groupId>
    <artifactId>interflight</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>interflight</name>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
    <relativePath/>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <feign-gson.version>7.2.1</feign-gson.version>
        <spring-cloud.version>Edgware.SR1</spring-cloud.version>
        <gson.version>2.8.0</gson.version>
        <assertj-core.version>3.8.0</assertj-core.version>
   <startClass>com.test.interflight.configuration.InterflightApplication</startClass>
  </properties>

    <dependencies>
    <!--External dependencies-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-feign</artifactId>
        </dependency>
        <dependency>
            <groupId>com.netflix.feign</groupId>
            <artifactId>feign-gson</artifactId>
            <version>${feign-gson.version}</version>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>${gson.version}</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

    <!--Test dependencies-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    <dependency>
      <groupId>org.assertj</groupId>
      <artifactId>assertj-core</artifactId>
      <version>${assertj-core.version}</version>
      <scope>test</scope>
    </dependency>
    </dependencies>

  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-dependencies</artifactId>
        <version>${spring-cloud.version}</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
        <configuration>
          <finalName>${project.artifactId}</finalName>
          <layout>ZIP</layout>
          <mainClass>com.test.interflight.configuration.InterflightApplication</mainClass>
        </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Output error:

    $ java -jar interflight.jar
Exception in thread "main" java.lang.ClassNotFoundException: com.test.interfl
ight.configuration.InterflightApplication
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at org.springframework.boot.loader.LaunchedURLClassLoader.loadClass(Laun
chedURLClassLoader.java:94)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner
.java:46)
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:87)
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:50)
        at org.springframework.boot.loader.PropertiesLauncher.main(PropertiesLau
ncher.java:587)

Main class:

@SpringBootApplication
@ComponentScan(basePackages = {"com.test.interflight.controller", "com.test.interflight.implementation.*"})
@EnableFeignClients(basePackages = {"com.test.interflight.api.restclient"},
                    defaultConfiguration = FeignConfiguration.class)
public class InterflightApplication {

    public static void main(String[] args) {
        SpringApplication.run(InterflightApplication.class, args);
    }
}

Short answer:

Looks like you're using the ZIP layout when you should be using the JAR layout. Change <layout>ZIP</layout> to <layout>JAR</layout> in your Spring Boot maven plugin declaration.

Longer answer:

Take a look at the docs which describe the ZIP layout as:

  • ZIP (alias to DIR ): similar to the JAR layout using PropertiesLauncher .

And of which the PropertiesLauncher is described in the docs as:

  • Launcher for archives with user-configured classpath and main class via a properties file...
  • Looks in various places for a properties file to extract loader settings, defaulting to application.properties... No default, but will fall back to looking for a Start-Class in a MANIFEST.MF

If you want it to be user specified in your Jar file, specify (or check and see if you have one already) in the meta-inf folder of your Jar, or specify the property yourself.

After several days dealing with various plugins, finally I could make a fat jar (spring boot) with externalize config files everywhere I wanted to be. You can use the following plugins:"config" is the path for your config files next to the jar file.

<plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <finalName>jarfilename</finalName>
                    <mainClass>com.start.YourStarter</mainClass>
                    <layout>ZIP</layout>
                </configuration>
                <executions>
                    <execution>
                        <phase>install</phase>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>**/log4j.xml</exclude>
                        <exclude>deploy/**</exclude>
                        <exclude>**/*.properties</exclude>
                        <exclude>**/*.txt</exclude>
                    </excludes>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <mainClass>com.start.YourStarter</mainClass>
                            <useUniqueVersions>false</useUniqueVersions>
                        </manifest>
                        <manifestEntries>
                            <Class-Path>config/</Class-Path>
                        </manifestEntries>
                    </archive>
                </configuration>
   </plugin>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM