[英]Error when running docker container “NoClassDefFoundError”
I am trying to dockerize a simple Spring Boot Application, built with Maven.我正在尝试使用 Maven 构建一个简单的 Spring 引导应用程序。
Dockerfile: Dockerfile:
FROM openjdk:latest
COPY target/backend-1.0-SNAPSHOT.jar app.jar
ENTRYPOINT ["java","-jar","app.jar"]
When I run the.jar without the container ( java -jar target/backend-1.0-SNAPSHOT.jar
), everything works fine and the app is running.当我在没有容器的情况下运行 .jar 时(
java -jar target/backend-1.0-SNAPSHOT.jar
),一切正常,应用程序运行正常。
Now I create the container with docker build -t company/backend.
现在我使用
docker build -t company/backend.
But when I try to run the docker container with docker run -p 8080:8080 company/backend
the following error occurs:但是,当我尝试使用
docker run -p 8080:8080 company/backend
运行 docker 容器时,会出现以下错误:
Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/boot/SpringApplication
at de.company.backend.Application.main(Application.java:10)
Caused by: java.lang.ClassNotFoundException: org.springframework.boot.SpringApplication
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:602)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
... 1 more
It seems like docker does not find the main class, even though it is defined in my pom.xml:似乎 docker 没有找到主要的 class,即使它是在我的 pom.xml 中定义的:
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<mainClass>de.elbdev.backend.Application</mainClass>
</properties>
<build>
<plugins>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>${mainClass}</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
Main Class:主要 Class:
package de.company.backend;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
In your pom.xml, the copy-dependencies
goal is specified at the install
phase: too late the package of the jar was already done.在您的 pom.xml 中,
copy-dependencies
项目标是在install
阶段指定的:jar 的 package 已经完成,为时已晚。
I am trying to dockerize a simple Spring Boot Application, built with Maven.
我正在尝试使用 Maven 构建一个简单的 Spring 引导应用程序。
You don't need to declare any plugin to create a fat jar with spring boot that could be run by a docker container.你不需要声明任何插件来创建一个胖 jar 启动 spring 可以由 docker 容器运行。
Declaring these plugins is error prone (and should be used only in corner cases) while the repackage
goal of the spring boot maven plugin attached by default to the package phase of maven will create for you the fat jar: Declaring these plugins is error prone (and should be used only in corner cases) while the
repackage
goal of the spring boot maven plugin attached by default to the package phase of maven will create for you the fat jar:
Repackages existing JAR and WAR archives so that they can be executed from the command line using java -jar
重新打包现有的 JAR 和 WAR 存档,以便可以使用 java -jar 从命令行执行它们
Juste remove these plugins declarations and execute mvn clean package
and it should be good.只需删除这些插件声明并执行
mvn clean package
应该没问题。
Side note:边注:
FROM openjdk:latest
来自 openjdk:最新
Don't use latest
as image version but favor a specific version of the image othewhise you could have bad surprises.不要使用
latest
版本作为图像版本,而是使用特定版本的图像,否则您可能会遇到意外。 As you use JDK 8, you could specify a JRE or a JDK 8 such as: FROM openjdk:8-jre-alpine
.当您使用 JDK 8 时,您可以指定 JRE 或 JDK 8,例如:
FROM openjdk:8-jre-alpine
。
I had the same problem as you.我和你有同样的问题。
you need to add plugin in your pom.xml.你需要在你的 pom.xml 中添加插件。
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
If you input as above, it works normally.如果按上述输入,则可以正常工作。
and check MANIFEST.MF (in.jar file)并检查 MANIFEST.MF(in.jar 文件)
Main-Class: org.springframework.boot.loader.JarLauncher
Start-Class: {your main class}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.