I try to run a simple java class with docker making the following steps:
create java file (HelloWorld.java)
compile it (javac HelloWorld.java)
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