Sping Boot docker Exception in thread“main”java.lang.NoClassDefFoundError:org/springframework/ Boot /SpringApplication

s3fp2yjn  于 2023-09-29  发布在  Spring
关注(0)|答案(3)|浏览(137)

我想在docker中启动我的spring-boot应用程序,但在docker run中出现错误
我的主类:

@SpringBootApplication
public class Application {

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

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

    <parent>
        <groupId>com.emo.test</groupId>
        <artifactId>test</artifactId>
        <version>0.1-SNAPSHOT</version>
    </parent>

    <artifactId>test-core</artifactId>
    <version>${parent.version}</version>
    <packaging>jar</packaging>

    <name>test-core</name>
    <url>http://maven.apache.org</url>

    <dependencies>
        ....
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <!-- Build an executable JAR -->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                            <mainClass>com.test.Application</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

我的货单:

Manifest-Version: 1.0
Built-By: emo.leumassi
Build-Jdk: 11.0.7
Main-Class: com.test.Application
Created-By: Apache Maven

由于某些原因,我不想使用spring-boot-maven-plugin
我的Dockerfile

FROM openjdk:8-jdk-alpine
ARG JAR_FILE=test-*.jar
COPY ${JAR_FILE} test.jar
ENTRYPOINT ["java", "-jar", "test.jar"]

我用这个脚本创建了容器:

docker build -t test .

docker run -p 8080:8080 test

我得到这个错误:

Sending build context to Docker daemon  18.94kB
Step 1/4 : FROM openjdk:8-jdk-alpine
 ---> a3562aa0b991
Step 2/4 : ARG JAR_FILE=test-*.jar
 ---> Running in 739ab5566b9f
Removing intermediate container 739ab5566b9f
 ---> 9d2e5a894070
Step 3/4 : COPY ${JAR_FILE} test.jar
 ---> 2746354c80a5
Step 4/4 : ENTRYPOINT ["java", "-jar", "test.jar"]
 ---> Running in 9850a06d6686
Removing intermediate container 9850a06d6686
 ---> e5ec33eac8a9
Successfully built e5ec33eac8a9
Successfully tagged kazi-core:latest
Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/boot/SpringApplication
        at com.test.Application.main(Application.java:10)
Caused by: java.lang.ClassNotFoundException: org.springframework.boot.SpringApplication
        at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
        ... 1 more
wljmcqd8

wljmcqd81#

maven-assembly-plugin添加到pom.xml
例如:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <configuration>
        <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
    </configuration>
    <executions>
        <execution>
            <id>make-assembly</id>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
</plugin>
hjqgdpho

hjqgdpho2#

使用spring Boot ,我们可以使用spring-boot-maven-plugin

<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <id>repackage</id>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                        <configuration>
                            <classifier>exec</classifier>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
92dk7w1h

92dk7w1h3#

检查你的项目构建gradle文件,如果你有这样的东西:

application {
   mainClass.set("MainKt")
}

只需将其删除并重新生成项目即可

相关问题