运行由spring-boot-maven-plugin配置的胖JAR时出现ClassNotFoundException

时间:2018-02-05 21:55:15

标签: java spring spring-boot classnotfoundexception spring-boot-maven-plugin

我正在尝试为我的spring启动应用程序创建可执行的JAR文件。 为了实现这个spring-boot-maven-plugin已经与指定的主类和jar包装一起使用。 不幸的是,在运行输出JAR文件后,我收到指向我的主类的java.lang.ClassNotFoundException。

下面是我的pom.xml和主类。在此先感谢您的帮助。

的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>

输出错误:

    $ 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)

主要课程:

@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);
    }
}

1 个答案:

答案 0 :(得分:1)

简答:

当您使用ZIP布局时,您似乎正在使用JAR布局。在Spring Boot maven插件声明中将<layout>ZIP</layout>更改为<layout>JAR</layout>

更长的回答:

查看描述ZIP布局的https://medium.com/@cereallarceny/server-side-rendering-with-create-react-app-fiber-react-router-v4-helmet-redux-and-thunk-275cb25ca972

  • ZIPDIR的别名):类似于使用的JAR布局 PropertiesLauncher

其中PropertiesLauncherthe docs为:

  • 用于配置用户配置的类路径和主类的归档的启动器 通过属性文件......
  • 查找属性文件的各个位置以提取加载程序设置,默认为application.properties ...没有默认值,但会回退到在MANIFEST.MF中查找 Start-Class

如果您希望在Jar文件中指定用户,请在Jar的meta-inf文件夹中指定(或检查并查看是否已有),或者自己指定属性。