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