Error while running the docker image | JNI error

huangapple go评论70阅读模式
英文:

Error while running the docker image | JNI error

问题

我在运行 Docker 镜像时遇到了一个错误。我使用 Gradle 创建了一个核心 Java 应用程序,使用 docker build 命令创建 Docker 镜像,但在运行 Docker 镜像时出现以下错误:

错误:发生了 JNI 错误,请检查您的安装并重试
主线程中的异常 "main" java.lang.NoClassDefFoundError: org/apache/kafka/clients/consumer/Consumer
...
Caused by: java.lang.ClassNotFoundException: org.apache.kafka.clients.consumer.Consumer
...

我可以看到这个错误是由于缺少 Kafka 消费者库引起的,但我不明白在 Docker 文件中漏掉了什么导致了这个问题。

以下是 Dockerfile 的内容:

FROM openjdk:8
ADD build/libs/*.jar parallel.jar

RUN bash -c 'touch /parallel.jar'
ENTRYPOINT ["java", "-jar", "parallel.jar"]

在运行 docker build 命令之前,我确保运行了 gradle clean build 命令。

以下是我在 build.gradle 中使用的依赖项:

implementation "io.confluent.parallelconsumer:parallel-consumer-core:0.5.2.4"
implementation "org.apache.avro:avro:1.11.1"
implementation "io.confluent:kafka-avro-serializer:3.3.0"
implementation "org.springframework.boot:spring-boot-starter-web"
implementation "org.codehaus.jackson:jackson-core-asl:1.9.13"
implementation "org.codehaus.jackson:jackson-mapper-asl:1.9.13"
implementation "org.apache.commons:commons-lang3:3.12.0"
implementation "org.slf4j:slf4j-simple:2.0.0"
implementation "me.tongfei:progressbar:0.9.3"
implementation 'org.awaitility:awaitility:4.2.0'
implementation("log4j:log4j:1.2.17")

希望这可以帮助您找出问题所在。

英文:

I am getting an error while running a docker image. I have created a core java application using gradle.

using docker build command to create the docker image and while running the docker image I am getting the following error

Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/kafka/clients/consumer/Consumer
        at java.lang.Class.getDeclaredMethods0(Native Method)
        at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
        at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
        at java.lang.Class.getMethod0(Class.java:3018)
        at java.lang.Class.getMethod(Class.java:1784)
        at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:650)
        at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:632)
Caused by: java.lang.ClassNotFoundException: org.apache.kafka.clients.consumer.Consumer
        at java.net.URLClassLoader.findClass(URLClassLoader.java:387)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:352)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
        ... 7 more

I can see that the error is due to missing kafka consumer library, but I am not able to understand what I have missed in the docker file which is causing this issue.

Here is the dockerfile

FROM openjdk:8
ADD build/libs/*.jar parallel.jar

RUN bash -c 'touch /parallel.jar'
ENTRYPOINT ["java", "-jar", "parallel.jar"]

Before I ran the docker build command, I made sure to run the gradle clean build command.

Here are the dependencies I have used in build.gradle

    implementation "io.confluent.parallelconsumer:parallel-consumer-core:0.5.2.4"
    implementation "org.apache.avro:avro:1.11.1"
    implementation "io.confluent:kafka-avro-serializer:3.3.0"
    implementation "org.springframework.boot:spring-boot-starter-web"
    implementation "org.codehaus.jackson:jackson-core-asl:1.9.13"
    implementation "org.codehaus.jackson:jackson-mapper-asl:1.9.13"
    implementation "org.apache.commons:commons-lang3:3.12.0"
    implementation "org.slf4j:slf4j-simple:2.0.0"
    implementation "me.tongfei:progressbar:0.9.3"
    implementation 'org.awaitility:awaitility:4.2.0'
    implementation("log4j:log4j:1.2.17")

答案1

得分: 1

Gradle默认不包含依赖项。在容器外运行 java jar 时会出现相同的错误。

您需要使用阴影插件将依赖项打包到您的JAR中。

或者,不要手动编写Dockerfile...使用 jib-gradle-plugin。此外,它将创建一个更优化的Docker镜像,不像阴影插件会创建Uber JAR那样。

英文:

Gradle doesn't include dependencies, by default. You'd get the same error when running java jar outside of the container.

You need to use shadow plugin to get dependencies packaged in your JAR.

Alternatively, don't write a Dockerfile manually... Use jib-gradle-plugin. Plus, it'll create a more optimized Docker image, which doesn't create an Uber JAR like the shadow plugin would.

huangapple
  • 本文由 发表于 2023年5月17日 18:39:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76271198.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定