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)