[英]Docker run returns an exception: Application has been compiled by a more recent version of the Java Runtime
I created a web application using Springboot and now I'm going to dockerize it and upload it into docker hub.我使用 Springboot 创建了一个 web 应用程序,现在我要将其 dockerize 并将其上传到 docker 集线器。 So my Dockerfile
is,所以我的Dockerfile
是,
FROM openjdk:8
EXPOSE 8080
ADD target/spring-boot-web-0.0.1-SNAPSHOT.jar spring-boot-web-0.0.1-SNAPSHOT.jar
ENTRYPOINT ["java","-jar","spring-boot-web-0.0.1-SNAPSHOT.jar"]
After creating .jar
inside my target I'm building docker image using the following command,在我的目标内创建.jar
后,我正在使用以下命令构建 docker 图像,
docker build -t kubernatesimage
It builds the docker image successfully and when I run the docker images
I can see the created image.它成功构建了 docker 映像,当我运行docker images
时,我可以看到创建的映像。 But before uploading it into docker hub I need to run and check so I'm executing,但在将其上传到 docker 集线器之前,我需要运行并检查,所以我正在执行,
docker run -it -p 4000:80 kubernatesimage
And this returns the following exception,这将返回以下异常,
Exception in thread "main" java.lang.UnsupportedClassVersionError: guru/springframework/SpringBootWebApplication has been compiled by a more recent version of the Java Runtime (class file version 55.0), this version of the Java Runtime only recognizes class file versions up to 52.0 Exception in thread "main" java.lang.UnsupportedClassVersionError: guru/springframework/SpringBootWebApplication has been compiled by a more recent version of the Java Runtime (class file version 55.0), this version of the Java Runtime only recognizes class file versions up to 52.0
According to @Nithin's answer in this StackOverflow question, I found this happens due to version missmatch and the java version codes,根据@Nithin 在此StackOverflow 问题中的回答,我发现这是由于版本不匹配和 java 版本代码导致的,
49 = Java 5
50 = Java 6
51 = Java 7
52 = Java 8
53 = Java 9
54 = Java 10
55 = Java 11
56 = Java 12
57 = Java 13
58 = Java 14
But still, I have no idea what do I need to perform to solve the issue.但是,我仍然不知道我需要执行什么来解决这个问题。 I mentioned openjdk:8 in my Dockerfile
and I run java -version
to get the local JDK version and it returned我在Dockerfile
中提到了 openjdk:8 并运行java -version
以获取本地 JDK 版本并返回
java version "1.8.0_271"
So do I need to change java version in my local machine or change my Dockerfile
?那么我需要在本地机器上更改 java 版本还是更改我的Dockerfile
?
Your application shouldnt run either way because of this docker run -it -p 4000:80 kubernatesimage
it should be docker run -it -p 4000:8080 kubernatesimage
您的应用程序不应该以任何一种方式运行,因为这个docker run -it -p 4000:80 kubernatesimage
它应该是docker run -it -p 4000:8080 kubernatesimage
Now concerning the issue: Your runtime version is 8: because of your dockerfile is "FROM openjdk:8" so your application will be running in java 8 environment =>version 52.. and you have compiled your application to jar file "spring-boot-web-0.0.1-SNAPSHOT.jar" by another version 55 which is java 11. So you have java version mismatch => The key is to make sure both the compile and runtime is using the same JDK. Now concerning the issue: Your runtime version is 8: because of your dockerfile is "FROM openjdk:8" so your application will be running in java 8 environment =>version 52.. and you have compiled your application to jar file "spring- boot-web-0.0.1-SNAPSHOT.jar" 由另一个版本 55,即 java 11。所以你有 java 版本不匹配 => 关键是要确保编译和运行时都使用相同的 JDK。
One proposed fix is change your java version in pom.xml file一个建议的修复方法是在 pom.xml 文件中更改您的 java 版本
<project>
...
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
...
</project>
Another fix is to change the runtime version in dockerfile to java 11, there is not possible to base your image on openjdk:11 however you can use this另一个修复方法是将 dockerfile 中的运行时版本更改为 java 11,无法将图像基于 openjdk:11 但是您可以使用它
FROM adoptopenjdk/openjdk11:alpine-jre
ARG JAR_FILE=target/*.jar
WORKDIR /opt/app
COPY ${JAR_FILE} app.jar
EXPOSE 8080
ENTRYPOINT ["java","-jar","app.jar"]
note: you can still run a smaller compiled java version in bigger runtime env, ie running a compiled 8 java version on java 11 runtime environment注意:您仍然可以在更大的运行时环境中运行较小的编译 java 版本,即在 java1 运行时环境中运行编译的 8 java 版本
I hope I helped我希望我有所帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.