-2

I try to run a simple java class with docker making the following steps:

  1. create java file (HelloWorld.java)

  2. compile it (javac HelloWorld.java)

  3. create dockerfile (above the content of the Dockerfile)

    FROM alpine:latest
    ADD HelloWorld.class HelloWorld.class
    RUN apk --update add openjdk8-jre
    ENTRYPOINT ["java", "-Djava.security.egd=file:/dev/./urandom", "HelloWorld"]
    

then I build it with this command:

   docker build --tag "docker-hello-world:latest" .

and I got the following:

    C:\Gradle>javac HelloWorld.java

    C:\Gradle>docker build --tag "docker-hello-world:latest" .
    => [internal] load build definition from Dockerfile
    => => transferring dockerfile: 209B
    => [internal] load .dockerignore
    => => transferring context: 2B
    => [internal] load metadata for docker.io/library/alpine:latest
    => [internal] load build context
    => => transferring context: 471B
    => [1/3] FROM docker.io/library/alpine:latest
    => CACHED [2/3] ADD HelloWorld.class HelloWorld.class
    => CACHED [3/3] RUN apk --update add openjdk8-jre
    => exporting to image
    => => exporting layers
   => => writing image sha256:e3450ba2f444321b7d9965d95443e1d394ef8c62549268059e9786bb882992ec
   => => naming to docker.io/library/docker-hello-world:latest   

I think everything is Ok, and I tried to run it with this command:

    docker run docker-hello-world:latest

an I get this Error:

    C:\Gradle>docker run docker-hello-world:latest
    Error: A JNI error has occurred, please check your installation and try again
    Exception in thread "main" java.lang.UnsupportedClassVersionError: HelloWorld has been compiled by a more recent version of the Java Runtime (class file version 59.0), this version of the Java Runtime only recognizes class file versions up to 52.0
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:757)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:468)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:74)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:369)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:363)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:362)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:419)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:352)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:352)
    at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:601)

this is my java version on my machine :

    C:\Gradle>java -version
    openjdk version "15.0.1" 2020-10-20
    OpenJDK Runtime Environment (build 15.0.1+9-18)
    OpenJDK 64-Bit Server VM (build 15.0.1+9-18, mixed mode, sharing)

and by the way I build my java file whit the same java version on my machine,therefore I don't know why the error said,the HelloWorld java file has been compiled by more recent version of java Runtime... My intention is just to run this Java file with docker and get <Hello,World> in the output

otmann
  • 37
  • 2
  • 17
  • What version of JDK did you use to compile the java application? Maybe java 9, 11, or 14 or even the latest? Because you specify in the docker container to use`java 8` – Aris Feb 23 '21 at 19:45
  • I compile it with openjdk version 15.0.1, and I updated it in the Dockerfile, but unfortunately without success.. – otmann Feb 23 '21 at 21:11
  • Does this answer your question? [Has been compiled by a more recent version of the Java Runtime (class file version 57.0)](https://stackoverflow.com/questions/10382929/how-to-fix-java-lang-unsupportedclassversionerror-unsupported-major-minor-versi) – rkosegi Feb 24 '21 at 13:45

1 Answers1

1

The error UnsupportedClassVersionError means that you are trying to execute a class compiled with the wrong version of java.

You have compiled the class with a recent java version (15), but you are running java 8 inside docker.

Upgrade the java version inside docker to java 15 will fix the error.

Edit: On linux Alpine openjdk15-jre is only available on testing repository.


FROM alpine:latest
ADD HelloWorld.class HelloWorld.class
RUN apk --update add openjdk15-jre --repository=http://dl-cdn.alpinelinux.org/alpine/edge/testing/
ENTRYPOINT ["java", "-Djava.security.egd=file:/dev/./urandom", "HelloWorld"]

Edit

Another possbile solution is to compile the java class to be compatible with java8.

To do this compile with the following command:

javac -source 8 -target 8 HelloWorld.java

Remember that if you compile to java8 you cannot use any new feature introduced after java8 like the new switch expression or records.

After that create the docker with the original configuration:

FROM alpine:latest
ADD HelloWorld.class HelloWorld.class
RUN apk --update add openjdk8-jre
ENTRYPOINT ["java", "-Djava.security.egd=file:/dev/./urandom", "HelloWorld"]
doscio
  • 89
  • 3
  • ....I don't know how to update java inside docker, I think the instruction to update it, is already in the Dockerfile in the 3 Line :RUN apk --update add openjdk15-jre, I've tried to do it in the command line, but with no success – otmann Feb 23 '21 at 21:05
  • After updating to java15 the error message is different? – doscio Feb 23 '21 at 21:10
  • => ERROR [3/3] RUN apk --update add openjdk15-jre – otmann Feb 23 '21 at 21:13
  • > [3/3] RUN apk --update add openjdk15-jre: – otmann Feb 23 '21 at 21:14
  • #7 0.669 fetch https://dl-cdn.alpinelinux.org/alpine/v3.13/main/x86_64/APKINDEX.tar.gz – otmann Feb 23 '21 at 21:14
  • #7 1.655 fetch https://dl-cdn.alpinelinux.org/alpine/v3.13/community/x86_64/APKINDEX.tar.gz – otmann Feb 23 '21 at 21:15
  • #7 4.354 ERROR: unable to select packages: – otmann Feb 23 '21 at 21:15
  • #7 4.393 openjdk15-jre (no such package): – otmann Feb 23 '21 at 21:16
  • #7 4.393 required by: world[openjdk15-jre] – otmann Feb 23 '21 at 21:16
  • executor failed running [/bin/sh -c apk --update add openjdk15-jre]: exit code: 1 – otmann Feb 23 '21 at 21:16
  • that was the whole error messages – otmann Feb 23 '21 at 21:17
  • Ok, i've looked at the alpine package and `openjdk15-jre` is present only for the edge version, that is the current development version. So you can upgrade your docker images to use alpine:edge `FROM alpine:edge` or you can recompile the java class and set the target to java8 – doscio Feb 23 '21 at 21:32
  • Now I change from opnejdk15 to the standard java with this version "15.0.2" 2021-01-19, and I pass it to the dockerfile file in this way: RUN apk --update add java15-jre, but it still not working, and it gives me this error now---> next comment – otmann Feb 23 '21 at 21:49
  • => CANCELED [2/3] RUN apk --update add java15-jre-------------=> ERROR [3/3] COPY docker-hello-world-example.jar app.jar---------------------- > [3/3] COPY docker-hello-world-example.jar app.jar:---------------------------failed to compute cache key: "/docker-hello-world-example.jar" not found: not found – otmann Feb 23 '21 at 21:50
  • The error means that the jar file is not present in the same folder as the Dockerfile – doscio Feb 24 '21 at 07:01
  • on my file system, I have those files: 1-Dockerfile, 2-HelloWorld.class, 3-HelloWorld.java, I make a quick search for it with no success – otmann Feb 24 '21 at 11:17
  • 1
    I've updated the answer to add the step to compile java15 code to java8. – doscio Feb 24 '21 at 13:44
  • @doscio........now it is working....but is it then possible only because I just compile it explicitly with the version 8 in the syntax you sent: .....I can't understand why? – otmann Feb 24 '21 at 15:26
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/229165/discussion-between-doscio-and-otmann). – doscio Feb 24 '21 at 15:35