I try to run java code on docker but I got an error that I could not solve could you please help me?!
I have a very simple java code which is to calculate the average.
import java.util.Scanner;
class Ave
{
public static void main(String args[])
{
int n;
double res=0;
Scanner reader=new Scanner(System.in);
System.out.println("Enter how many numbers to calculate the avrage ");
n=reader.nextInt();
int a[]=new int[n];
System.out.println("Enter "+n+" numbers");
for(int i=0;i<n;i++)
a[i]= (int) reader.nextDouble();
res=Ave.CalAvg(a,n);
System.out.println("The average is " +res/n);
}
static double CalAvg(int a[],int n)
{
double res=0;
for(int i=0;i<n;i++)
res =res+a[i];
return res;
}
}
My Dockerfile is
FROM alpine:latest
ADD Ave.class Ave.class
RUN apk --update add openjdk8-jre
ENTRYPOINT ["java", "-Djava.security.egd=file:/dev/./urandom", "Ave"]
What I did that I compiled this file using the Java compiler.
$ javac Ave.java
I used below command to build an image from this Dockerfile
$ docker build --tag "docker-hello-world:latest" .
Then, I tried to run the Docker image to see the result by executing the below command.
$ docker run docker-hello-world:latest
Finally, I got this error or exception which I could not figure out
The code is run on the server and local machine but it does not work on docker
Enter how many numbers to calculate the avrage
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:862)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at Ave.main(Ave.java:17)
The result
Try following
My env: Mac-Os Sierra-10.12.6
Docker version:
Docker version 18.03.0-ce, build 0520e24
To Check Java version in container:
execute $docker run -it docker-hello-world:latest
get ContainerID by executing $docker ps
get shell of docker image docker exec -i -t <CONTAINER ID>
Execute following command
$java -version
openjdk version "1.8.0_151"
OpenJDK Runtime Environment (IcedTea 3.6.0) (Alpine 8.151.12-r0)
OpenJDK 64-Bit Server VM (build 25.151-b12, mixed mode)
Create a directory (anyname here I gave a
), I created at Desktop,
cd ~/Desktop
mkdir a
create Ave.java
file in ~/Desktop/a
directory
Also, have Dockerfile in the same directory(/a)
Considering there is no package name in Ave.java
$javac Ave.java
now folder /a will have 3 files, Ave.java, Ave.class, Dockerfile
Execute following command
$docker build -t docker-hello-world:latest .
Console Logs:
Sending build context to Docker daemon 5.632kB
Step 1/4 : FROM alpine:latest
---> 3fd9065eaf02
Step 2/4 : ADD Ave.class Ave.class
---> 8b94ae6de674
Step 3/4 : RUN apk --update add openjdk8-jre
---> Running in f12eb4589a34
fetch http://dl-cdn.alpinelinux.org/alpine/v3.7/main/x86_64/APKINDEX.tar.gz
fetch http://dl-cdn.alpinelinux.org/alpine/v3.7/community/x86_64/APKINDEX.tar.gz
(1/39) Installing libffi (3.2.1-r4)
(2/39) Installing libtasn1 (4.12-r3)
(3/39) Installing p11-kit (0.23.2-r2)
(4/39) Installing p11-kit-trust (0.23.2-r2)
(5/39) Installing ca-certificates (20171114-r0)
(6/39) Installing java-cacerts (1.0-r0)
(7/39) Installing libxau (1.0.8-r2)
(8/39) Installing libbsd (0.8.6-r1)
(9/39) Installing libxdmcp (1.1.2-r4)
(10/39) Installing libxcb (1.12-r1)
(11/39) Installing libx11 (1.6.5-r1)
(12/39) Installing libxcomposite (0.4.4-r1)
(13/39) Installing libxext (1.3.3-r2)
(14/39) Installing libxi (1.7.9-r1)
(15/39) Installing libxrender (0.9.10-r2)
(16/39) Installing libxtst (1.2.3-r1)
(17/39) Installing alsa-lib (1.1.4.1-r2)
(18/39) Installing libbz2 (1.0.6-r6)
(19/39) Installing libpng (1.6.34-r1)
(20/39) Installing freetype (2.8.1-r2)
(21/39) Installing libgcc (6.4.0-r5)
(22/39) Installing giflib (5.1.4-r1)
(23/39) Installing libjpeg-turbo (1.5.2-r0)
(24/39) Installing libstdc++ (6.4.0-r5)
(25/39) Installing openjdk8-jre-lib (8.151.12-r0)
(26/39) Installing java-common (0.1-r0)
(27/39) Installing krb5-conf (1.0-r1)
(28/39) Installing libcom_err (1.43.7-r0)
(29/39) Installing keyutils-libs (1.5.10-r0)
(30/39) Installing libverto (0.3.0-r0)
(31/39) Installing krb5-libs (1.15.2-r1)
(32/39) Installing lcms2 (2.8-r1)
(33/39) Installing nspr (4.17-r0)
(34/39) Installing sqlite-libs (3.21.0-r0)
(35/39) Installing nss (3.34.1-r0)
(36/39) Installing pcsc-lite-libs (1.8.22-r0)
(37/39) Installing lksctp-tools (1.0.17-r0)
(38/39) Installing openjdk8-jre-base (8.151.12-r0)
(39/39) Installing openjdk8-jre (8.151.12-r0)
Executing busybox-1.27.2-r7.trigger
Executing ca-certificates-20171114-r0.trigger
Executing java-common-0.1-r0.trigger
OK: 81 MiB in 50 packages
Removing intermediate container f12eb4589a34
---> 82d9ecfcc95e
Step 4/4 : ENTRYPOINT ["java", "-Djava.security.egd=file:/dev/./urandom", "Ave"]
---> Running in 28f2df6fb544
Removing intermediate container 28f2df6fb544
---> bbf098575e6a
Successfully built bbf098575e6a
Successfully tagged docker-hello-world:latest
Execute command as suggested by @Siking
$docker run -it docker-hello-world:latest
here is snapshot of output:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With