简体   繁体   中英

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. So my Dockerfile is,

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,

docker build -t kubernatesimage

It builds the docker image successfully and when I run the docker images I can see the created image. But before uploading it into docker hub I need to run and check so I'm executing,

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

According to @Nithin's answer in this StackOverflow question, I found this happens due to version missmatch and the java version codes,

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

java version "1.8.0_271"

So do I need to change java version in my local machine or change my 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

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.

One proposed fix is change your java version in pom.xml file

<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

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

I hope I helped

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM