Reputation: 347
I am struggling to compile and run a java file using docker. I have the file structure:
./Repo/
Dockerfile
./libs/
org.json.jar
./out/
Program.class
./src/
Program.java
My Dockerfile looks something like this:
FROM java:8-jdk-alpine
COPY /src /src/
COPY /out /out/
COPY /libs /libs/
RUN javac ..
CMD java ..
I want to be able to compile the files in src and output them in out whilst using the external libs The only way I have managed to do this is to first CD into /src/ and run the following:
javac -d ../out -cp ../libs/org.json.jar Program.java
But surely there is a way to do this from the directory that the Dockerfile is in like?
javac -d /out -cp /libs/org.json.jar /out/Program.java
The next issue is the CMD required to run the above program. I'm not sure how to write this.
Or should I copy the json lib to the out folder where the compiled program is and run the following CMD:
java -cp "org.json.jar;" Server
Upvotes: 0
Views: 3387
Reputation: 191681
You're trying to compile the Program.java file from the /out path instead of the src, which is probably why it wasn't working.
Try this
RUN javac -d /out -cp /libs/*.jar /src/Program.java
Then you'll not need to copy out folder because it'll be created when the container is built. When you run docker build
, things should compile
Otherwise trying to run javac outside the container, /out and /src don't exist on your source machine, so you must use relative paths in the command, then copy the out folder only in the Dockerfile and remove the javac usage (and switch to using the JRE image)
Upvotes: 2