I have a Java project in IntelliJ. My Main class as well as other classes are in the default subfolder src/com/company. (Not sure why, that’s just how IntelliJ sets up my folders).
I have tried creating the Dockerfile in the project root folder. The Dockerfile has no extension and contains the following text:
FROM openjdk:11 WORKDIR /app COPY . /app RUN javac *.java CMD java Main
From the project root folder, I then try to build the image:
docker build -t food-quick ./
This gives me the error:
#7 0.406 error: file not found: *.javarom Dockerfile
Then, I tried changing the Main to the fully classified classname, like so:
FROM openjdk:11 WORKDIR /app COPY . /app RUN javac *.java CMD java com.company.Main
But now when I try to build, it gives this error:
#8 0.371 error: file not found: *.javar.io/library/openjdk:11
So then I tried moving the Dockerfile into the project/src/com/company folder right next to the classes I have in there.
I now cd to that directory and try to build again. It now works.
But now when I try to run the image, it gives me:
Error: Could not find or load main class com.company.Main Caused by: java.lang.ClassNotFoundException: com.company.Main
And when I change the text in the Dockerfile back to
CMD java Main, it gives me this error when I try to run the file:
Error: Could not find or load main class Main Caused by: java.lang.NoClassDefFoundError: com/company/Main (wrong name: Main)
I get these errors also when I try to run the image with
docker run food-quick.
I have also tried doing this directly in IntelliJ by downloading the Docker plugin and following these steps: Run a Java application in a Docker container | IntelliJ IDEA.
But then I get this error message:
Error response from daemon: COPY failed: file not found in build context or excluded by .dockerignore: stat out/production/Main/: file does not exist Failed to deploy '<unknown> Dockerfile: Dockerfile': Can't retrieve image ID from build stream
I think there must be something wrong with how my project folders are set up and how Docker is trying to compile and find my classes. But after searching on this forum, IntelliJ and Google, I still can’t figure out how to fix it.
Anyone able to offer some ideas?