在docker中启动helloWorld.java时出错

时间:2018-09-15 20:16:59

标签: java docker

我尝试在hava上创建helloWorld应用程序,然后在Windows的docker中启动它。

  1. 我编写应用程序:

    public class HelloWorld {
        public static void main(String[] args) {
            System.out.println("Start... ");
            System.out.println("Hello Pavel!");
        }
    }
    
  2. 我创建Docker文件:

    FROM java:8
    ADD HelloWorld.java .
    RUN javac HelloWorld.java
    CMD ["java", "HelloWorld"]
    
  3. 我放入java-application文件夹中的文件:

    -java-application
    -HelloWorld.java
    -Dockerfile 
    
  4. 我尝试构建它:

Pavel\java-application>docker build -t java-application .
Sending build context to Docker daemon  3.072kB
Step 1/4 : FROM java:8
 ---> d23bdf5b1b1b
Step 2/4 : ADD HelloWorld.java .
 ---> Using cache
 ---> d4e0c540b57b
Step 3/4 : RUN javac HelloWorld.java
 ---> Using cache
 ---> 5f5204f28710
Step 4/4 : CMD ["java", "HelloWorld"]
 ---> Using cache
 ---> 53ebab34502c
Successfully built 53ebab34502c
Successfully tagged java-application:latest
SECURITY WARNING: You are building a Docker image from Windows against a non-Windows Docker host. All files and directories added to build context will have '-rwxr-xr-x' permissions. It is recommended to double check and reset permissions for sensitive files and directories.

我显示了所有图像:

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
java-application    latest              53ebab34502c        2 minutes ago       643MB
hello-world         latest              4ab4c602aa5e        8 days ago          1.84kB
ubuntu              latest              cd6d8154f1e1        9 days ago          84.1MB
nginx               latest              06144b287844        10 days ago         109MB
java                8                   d23bdf5b1b1b        20 months ago       643MB
  1. 我尝试启动应用程序:

    C:\ Pavel \ java-application> docker运行Java应用程序 错误:找不到或加载主类HelloWorld

EDDIT

我更改dockerfile:

FROM openjdk:8
ADD HelloPavel.java .
RUN javac HelloPavel.java
ENTRYPOINT ["java", "HelloPavel"]

我有

hello-pavel         latest              c817bd40d62c        25 seconds ago      624MB

我开始:

C:\Pavel\hello-pavel>docker run hello-pavel:latest
Error: Could not find or load main class HelloPavel

1 个答案:

答案 0 :(得分:0)

错误非常清楚,类Helloworld不在docker内部的java类路径中。用

添加
java -cp . HelloPavel

当您第一次将目录更改为编译类所在的位置时。