Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

888
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda