Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

889
Views
Docker to Run Small Java Class: Could Not Find Or Load Main Class (Even Though It Exists)

I realize that this question may already have been asked, but in my research I can't find an answer. I'm probably making a simple mistake.

I would like to run a Java class locally with Docker, inside a container. Below is my Dockerfile:

Dockerfile

FROM maven:3.5.2-jdk-8

COPY src /src

RUN javac src/java/com/Main.java
CMD java src/java/com/Main

I then run these commands in order:

docker build -t my_image_6_26_19:latest .
docker run -it my_image_6_26_19:latest

The build command runs fine, but the run command throws the following error:

Error: Could not find or load main class src.java.com.Main

I have reviewed the following questions on SO, but no answers seem to work (or maybe I didn't catch the solution):

  • Buildning docker image from Dockerfile with maven - Error: "Could not find or load main class"
  • DockerFile to run a java program
  • Docker Error: Could not find or load Main class Main.Main

When I build the container, through some debugging (RUN ls /src/java/com) I can see that a file Main.class is being created. I'm not sure why that file can't be found. Additionally, I have tried changing the last line of my Dockerfile to CMD java src/java/com/Main.class, but no luck.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

If Main doesn't have a package try:

 CMD java -classpath src/java/com Main

if it does have a package (e.g. com, perhaps?) try:

CMD java -classpath src/java com.Main
over 4 years ago · Santiago Trujillo Report

0

The second version in answer from @martijno is the corect one. I additionally recommend telling the compiler to write the output into another directory, e.g. bin:

RUN javac src/java/com/Main.java -d bin
CMD java -cp bin com.Main
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!