简体   繁体   English

Docker 运行失败并出现 NoClassDefFoundError - 即使依赖 jar 在类路径中

[英]Docker run fails with NoClassDefFoundError - even though dependant jar is in classpath

I am a docker newbie.我是 docker 新手。 I am trying to run a java class in the container.我正在尝试在容器中运行 java class 。 My main class is depending on a local jar.我的主要 class 取决于本地 jar。 Am able to add the jar to the docker image and able set the jar in the classpath.我能够将 jar 添加到 docker 映像中,并且能够在类路径中设置 jar。

But still, when run the container it fails with "Exception in thread "main" java.lang.NoClassDefFoundError"但是,当运行容器时,它仍然失败,并出现“线程“主”java.lang.NoClassDefFoundError 中的异常”

My Dockerfile我的 Dockerfile

FROM java:8

WORKDIR /

ADD Test.jar Test.jar

ADD Dependant.jar Dependant.jar

RUN mkdir /usr/myjars

COPY /Dependant.jar /usr/myjars/Dependant.jar

ENV CLASSPATH .:/usr/myjars/Dependant.jar

RUN export CLASSPATH=.:/usr/myjars/Dependant.jar

CMD ["java",  "-jar", "Test.jar"]

Please help me to identify issue请帮我确定问题

Your Dockerfile is more complex than needed.您的Dockerfile比需要的复杂。 You could:你可以:

FROM java:8

COPY Test.jar /

ENV CLASSPATH=/usr/myjars
RUN mkdir -p ${CLASSPATH}
COPY Dependant.jar ${CLASSPATH}

# You need the SHELL form of CMD or ENTRYPOINT because you're using ENV
ENTRYPOINT java -jar /Test.jar

Normally when we try to run a java class from a JAR file, JVM won't consider the environment CLASSPATH.通常,当我们尝试从 JAR 文件中运行 java class 时,Z18B5A217C4DAD256D62D 不会考虑环境Instead it will look class path defnition of manifest file in the jar.相反,它将在 jar 中查看清单文件的 class 路径定义。

Refer: https://docs.oracle.com/javase/tutorial/deployment/jar/downman.html参考: https://docs.oracle.com/javase/tutorial/deployment/jar/downman.html

Considering the case, I have corrected the creation of my CheckSum.jar/Dependant.jar with following manifest.考虑到这种情况,我已经使用以下清单更正了我的 CheckSum.jar/Dependant.jar 的创建。

MANIFEST.MF清单文件

Manifest-Version: 1.0
Class-Path: . /usr/myjars/PaytmChecksum.jar
Main-Class: TestChecksumGeneration

following is my corrected Docker file.以下是我更正后的 Docker 文件。

Dockerfile Dockerfile

FROM java:8

COPY CheckSum.jar /

ENV JARDIR=/usr/myjars

RUN mkdir -p ${JARDIR}

COPY PaytmChecksum.jar ${JARDIR}

ENTRYPOINT java -jar /CheckSum.jar

Note : I have not provided any CLASSPATH to my docker container.注意:我没有为我的 docker 容器提供任何CLASSPATH

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 NoClassDefFoundError 即使 Jar 存在于类路径中 - NoClassDefFoundError even though Jar is present in classpath classpath缺少jar文件,即使指定了 - classpath missing jar file even though specified 即使包含.jar文件,也不会出现NoClassDefFoundError - NoClassDefFoundError even though .jar file is included jGrasp即使在类路径中也找不到jar文件 - jGrasp can't find jar file even though it's in the classpath ClassNotFoundException,即使包含该类的jar正确存在于类路径中 - ClassNotFoundException even though the jar containing the class is properly present in the classpath 即使在清单类路径中包含了JAR,也不会出现NoClassDefFoundError - NoClassDefFoundError even though the JAR's are included in Manifest Class-Path Java: simple JAR project, when run, cannot find an imported class in a second simple JAR project even though second JAR passed via -classpath - Java: simple JAR project, when run, cannot find an imported class in a second simple JAR project even though second JAR passed via -classpath 在类路径上带有jar的Tomcat webapp NoClassDefFoundError - Tomcat webapp NoClassDefFoundError with jar on classpath Ant JavaDoc任务从自定义标记中抛出`NoClassDefFoundError`,即使类*在类路径上是* - Ant JavaDoc task throwing `NoClassDefFoundError` from within a custom taglet, even though the class *is* on the classpath 使用Guava类时出现NoClassDefFoundError,即使Guava包含在Gradle构建文件和类路径中 - NoClassDefFoundError when using Guava classes even though Guava is included in Gradle build file and on classpath
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM