简体   繁体   English

运行 docker 容器“NoClassDefFoundError”时出错

[英]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.

相关问题 语法错误:“(”在运行 Docker 容器时出现意外 - Syntax error: "(" unexpected when running Docker container 从Docker容器运行SparkApp反对在另一个容器中运行Spark时出错 - Error when running SparkApp from docker container against Spark running in another container 仅在本地运行时出现NoClassDefFoundError错误 - NoClassDefFoundError error when running locally only 连接到在 docker 容器中运行的 Kafka 时出错 - Error connecting to Kafka running in docker container 运行程序时出现NoClassDefFoundError - NoClassDefFoundError when running program 在linux中运行时NoClassDefFoundError - NoClassDefFoundError when running in linux 运行时NoClassDefFoundError Jar - NoClassDefFoundError when running Jar 在Docker容器中运行时从属性文件实例化Spring Configuration类时出错 - Error instantiating Spring Configuration class from properties file when running in Docker container IO 错误:将 oracle DB 作为 docker 容器运行时,网络适配器无法建立连接 - IO Error: The Network Adapter could not establish the connection when running oracle DB as docker container 错误:从NodeJs运行Java时出现java.lang.NoClassDefFoundError - Error: java.lang.NoClassDefFoundError when running java from NodeJs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM