2

I try create helloWorld application on hava and start it in docker on windows.

  1. I write application:

    public class HelloWorld {
        public static void main(String[] args) {
            System.out.println("Start... ");
            System.out.println("Hello Pavel!");
        }
    }
    
  2. I create Docker file:

    FROM java:8
    ADD HelloWorld.java .
    RUN javac HelloWorld.java
    CMD ["java", "HelloWorld"]
    
  3. Bouth files I put to java-application folder:

    -java-application
    -HelloWorld.java
    -Dockerfile 
    
  4. I try build it:

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.

I show all images:

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. I try start application:

    C:\Pavel\java-application>docker run java-application Error: Could not find or load main class HelloWorld

EDDIT

I change dockerfile:

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

I have

hello-pavel         latest              c817bd40d62c        25 seconds ago      624MB

I start:

C:\Pavel\hello-pavel>docker run hello-pavel:latest
Error: Could not find or load main class HelloPavel
ip696
  • 6,574
  • 12
  • 65
  • 128
  • Please switch from [java](https://hub.docker.com/_/java/) image to [openjdk](https://hub.docker.com/_/openjdk/) image as it is marked as deprecated. Furthermore, you should use `ENTRYPOINT` instead of `CMD` as this is the right way to create executable images according to [official documentation](https://docs.docker.com/develop/develop-images/dockerfile_best-practices/#entrypoint). – dominikbrandon Sep 15 '18 at 20:40
  • I use this tutorial https://www.tutorialkart.com/docker/docker-Java-application-example/ – ip696 Sep 15 '18 at 20:41

1 Answers1

0

The error is very clear, the class Helloworld is not in your java classpath inside docker. Add it with

java -cp . HelloPavel

When you first changed your directory to where your compiled class is.

Bert Verhees
  • 1,057
  • 3
  • 14
  • 25
  • Where do I add this line? Run it in the console, add it to the dockerfile? – ip696 Sep 15 '18 at 20:46
  • Check this: http://www.sergiy.ca/how-to-compile-and-launch-java-code-from-command-line/ and this: https://stackoverflow.com/questions/30494050/how-do-i-pass-environment-variables-to-docker-containers – Bert Verhees Sep 15 '18 at 20:49
  • it's still hard to understand – ip696 Sep 15 '18 at 20:59
  • So there are two choices. The problem is you do not have the class you call in your classpath inside docker. Choice one is to add the classpath as part of your java-command. For that purpose is the -cp parameter, and the other possibility is to add the path to your classpath as part of the environment inside docker. Because docker is normally used to run a single program, both ways to do it are good. But first try to run it outside docker, study the classpath if it runs, and see what is different inside docker. You learn most if you can figure it out without further help. Success – Bert Verhees Sep 15 '18 at 21:19