在docker上运行java代码

时间:2018-04-20 23:51:56

标签: java unix docker dockerfile

我尝试在docker上运行java代码,但是我得到了一个我无法解决的错误,请你帮忙吗?!

我有一个非常简单的java代码来计算平均值。

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;
        }
}

我的Dockerfile是

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

我做了什么,我使用Java编译器编译了这个文件。

$ javac Ave.java

我使用下面的命令从这个Dockerfile构建一个图像

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

然后,我尝试运行Docker镜像,通过执行以下命令来查看结果。

$ docker run docker-hello-world:latest

最后,我得到了这个我无法弄清楚的错误或异常

代码在服务器和本地计算机上运行但在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

1 个答案:

答案 0 :(得分:0)

尝试以下

我的环境:Mac-Os Sierra-10.12.6

Docker版本:

Docker version 18.03.0-ce, build 0520e24

容器中的Java版本(1.执行$docker run -it docker-hello-world:latest,2。通过执行$docker ps获取ContainerID 3.获取docker image docker exec -i -t <CONTAINER ID>的shell:

$java -version
  

openjdk version“1.8.0_151”

     

OpenJDK运行时环境(IcedTea 3.6.0)(Alpine 8.151.12-r0)

     

OpenJDK 64位服务器VM(版本25.151-b12,混合模式)

创建一个目录(我在这里给的任何名字a),我在Desktop创建,

cd ~/Desktop
mkdir a

Ave.java目录

中创建~/Desktop/a文件

此外,将Dockerfile放在同一目录(/ a)

考虑到Ave.java中没有包名称

$javac Ave.java

现在 文件夹/ a将有3个文件,Ave.java,Ave.class,Dockerfile

执行以下命令

$docker build -t docker-hello-world:latest .

控制台日志:

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

执行@Siking

建议的命令
$docker run -it docker-hello-world:latest

这里是输出的快照: enter image description here