1

I want to create a docker with java and test it, however I get an error

This is the docker file

FROM openjdk:8-jdk-slim

…



COPY  Simple.java /project/


WORKDIR /project

CMD ["java" ,”--version”]

I try to run the file Simple.java

class Simple{
    public static void main(String args[]){
        System.out.println("Hello Java");
    }
}

when I run the following command:

docker run mydocker javac Simple.java

I get the error:

Simple.java:1: error: error while writing Simple: Simple.class (Permission denied)
class Simple{
^
1 error

How could I test a simple java program and see that it works?

1
  • in your Simple.java change class name to public class Simple. Commented Sep 19, 2019 at 8:47

1 Answer 1

4

You are overriding the command(CMD) in dockerfile when you run

docker run mydocker  javac Simple.java

Here what docker doc says:

This command is optional because the person who created the IMAGE may have already provided a default COMMAND using the Dockerfile CMD instruction. As the operator (the person running a container from the image), you can override that CMD instruction just by specifying a new COMMAND

I changed your Dockerfile a bit, here:

FROM openjdk:8-jdk-slim

COPY  Simple.java /project/

WORKDIR /project

RUN ["javac" ,"Simple.java"]
CMD ["java" ,"Simple"]

Now you should be able to run

docker run mydocker

Output;

➜ DockerTest docker run mydocker
Hello Java
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.