1

I am trying to run the docker image in AWS cloud so in the local ,compiled the application with jdk-11.0.1 and written docker file like this; Please note that I have not installed any JDK in AWS ec2 instance because I have already included adoptopenjdk/openjdk11:latest image in the final image..

FROM adoptopenjdk/openjdk11:latest
ADD target/demo-0.0.1-SNAPSHOT.jar app.jar
ENTRYPOINT ["java","-jar","app.jar"]
EXPOSE 8080

1)create the docker image
2)pushed the docker image to docker hub
3)when I run the docker container then getting below error.

Exception in thread "main" java.lang.UnsupportedClassVersionError: com/example/demo/DemoApplication 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

user739115
  • 1,117
  • 5
  • 20
  • 41
  • 1
    Version 55 is JDK 11. That's as expected as you have compiled with JDK 11. Version 52 is JDK 8. So even though you have included OpenJDK 11 in the Docker image, it seems that the code is run with JDK 8 and therefore fails. – Codo Sep 21 '20 at 10:53
  • then what is the solution for this? – user739115 Sep 21 '20 at 13:12
  • Your docker image contains JDK 8 instead of JDK 11. Check the entire CI/CD pipeline to find where it's going wrong. I'm pretty sure the above code is correct and if built and run locally it will work. – Codo Sep 22 '20 at 06:21

3 Answers3

1

Most probably java got updated in local so you are compiling with a different version than you think. Or the local version is different than "adoptopenjdk/openjdk11:latest".

I suggest you use a multi-stage build so that your classes are always compiled against the same java version.

How you write your build stage depends on your environment so I don't have enough information to post an example. But basically you need to replicated everything you do in local inside the build stage. Then you just copy the jar to the last stage. And this will always work.

Mihai
  • 9,526
  • 2
  • 18
  • 40
0

Try changing your openjdk version to latest i.e FROM:openjdk:12 work for me as i am using latest java version, use respective openjdk versions to match at runtime

0

To avoid this kind of issue It is recommend to be very specific about the images.

i.e. FROM adoptopenjdk/openjdk11:jre-11.0.9.1_1-alpine

For this kind of best practices I would suggest to go through this article I found while looking for the solution.

srp
  • 619
  • 7
  • 18