java Spring Boot 部署 - NoClassDefFoundError

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33166447/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 21:17:16  来源:igfitidea点击:

Spring Boot deployment - NoClassDefFoundError

javaspringmavendeploymentspring-boot

提问by Someone

I'm trying to deploy Spring Boot application. I build a jar file through maven plugins. But I get an error saying:

我正在尝试部署 Spring Boot 应用程序。我通过 Maven 插件构建了一个 jar 文件。但我收到一条错误消息:

Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/boot/SpringApplication
        at com.spring-boot.example.AppConfig.main(AppConfig.java:18)
Caused by: java.lang.ClassNotFoundException: org.springframework.boot.SpringApplication
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 1 more

I tried adding additional plugins to the pom.xml file, but none of it worked. Any help will be very appreciated.

我尝试在 pom.xml 文件中添加额外的插件,但都没有奏效。任何帮助将不胜感激。

This is my pom.xml file:

这是我的 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.spring-boot.example</groupId>
    <artifactId>spring-boot-example</artifactId>
    <version>0.1</version>

    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <start-class>com.spring-boot.example.AppConfig</start-class>

        <spring.version>4.0.7.RELEASE</spring.version>
        <log4j.version>1.2.17</log4j.version>
        <jdk.version>1.7</jdk.version>
    </properties>

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

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>4.2.0.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
            <version>1.2.1.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>


    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

回答by abarisone

First of all I would suggest you to upgrade your dependecies to the latest version of the Spring Boot framework, if applicable.

首先,如果适用,我建议您将依赖项升级到最新版本的 Spring Boot 框架。

Besides, independently from the version you're using, the class you're missing is contained into this dependency:

此外,独立于您使用的版本,您缺少的类包含在此依赖项中:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot</artifactId>
    <version>1.2.6.RELEASE</version>
</dependency>

but remember that all Spring Boot dependencies rely on Spring framework transitive dependencies, so check in your IDE's dependency hierarchy panel if they are present.

但请记住,所有 Spring Boot 依赖项都依赖于 Spring 框架的传递依赖项,因此请检查 IDE 的依赖项层次结构面板是否存在。

Depending on the way you're deploying your application you must be sure that all dependencies are available at runtime.

根据您部署应用程序的方式,您必须确保所有依赖项在运行时都可用。

This can be obtained using, for example, the repackagegoal into the spring-boot-maven-pluginin your pom's buildsection:

这可以使用,例如,在你的 pom部分中的repackage目标来获得:spring-boot-maven-pluginbuild

<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>${spring-boot-version}</version>
                <configuration>
                    <mainClass>your-main-class</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
       </plugins>
</build>

回答by Daniel Hajduk

I would suggest using maven assembly plugin so that all dependencies will be present in jar file after it is built. Here is documentation for it: http://maven.apache.org/plugins/maven-assembly-plugin/

我建议使用 maven assembly 插件,以便在构建后所有依赖项都将出现在 jar 文件中。这是它的文档:http: //maven.apache.org/plugins/maven-assembly-plugin/

回答by Shailendra Suriyal

try $ mvn dependency:purge-local-repositoryfrom your terminal. It may be because some of your dependency got corrupted. This command worked for me

$ mvn dependency:purge-local-repository从您的终端尝试 。这可能是因为您的某些依赖项已损坏。这个命令对我有用