I am trying to run java code on docker, but I have an error that I could not solve, could you help me ?!
I have very simple Java code which consists in calculating the average value.
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 docker file
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, I compiled this file using the Java compiler.
$ javac Ave.java
I used the command below to create 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 running the following command.
$ docker run docker-hello-world:latest
Finally, I received this error or exception that I could not understand
The code runs on the server and the local computer, 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)
Result
source
share