Skip to content

How to resolve Error: Could not find or load main class error when using docker with springboot

Problem

When using Docker with Spring Boot projects, you might encounter the following error:

Terminal window
bin git:(main) ./docker-run.sh myharbor.com/bswen/spring
Error: Could not find or load main class com.bswen.app7.Main

The core exception is:

Terminal window
Error: Could not find or load main class com.bswen.app7.Main

Environment

  • Java 1.8
  • Spring Boot 2.3
  • IntelliJ IDEA
  • Docker 19

Debug

Dockerfile

Dockerfile
FROM openjdk:8-jdk-alpine
ENV APPROOT="/opt/app7"
ARG DEPENDENCY=build/dependency
COPY ${DEPENDENCY}/BOOT-INF/lib ${APPROOT}/lib
COPY ${DEPENDENCY}/META-INF ${APPROOT}/META-INF
COPY ${DEPENDENCY}/BOOT-INF/classes ${APPROOT}
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-cp","/opt/app8:/opt/app7/lib/*","-Droot.dir=/opt/app7","com.bswen.app7.Main"]
EXPOSE 8082

Main Class

Main.java
package com.bswen.app7;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class Main {
public static void main(String[] args) {
SpringApplication.run(Main.class,args);
}
}

If you run the app with Gradle directly like this:

Terminal window
./gradlew app7:bootRun

It works. However, if you run the Spring Boot Docker image directly without Kubernetes like this:

Terminal window
docker run app7:latest

You get the error again:

Terminal window
Error: Could not find or load main class com.bswen.app7.Main

Reason

The issue lies in the Dockerfile configuration, which causes the Spring Boot application to start with an incorrect classpath.

Solution

The problem is a typo in the Dockerfile. The classpath should point to /opt/app7, not /opt/app8. Here’s the corrected Dockerfile:

Dockerfile
FROM openjdk:8-jdk-alpine
ENV APPROOT="/opt/app7"
ARG DEPENDENCY=build/dependency
COPY ${DEPENDENCY}/BOOT-INF/lib ${APPROOT}/lib
COPY ${DEPENDENCY}/META-INF ${APPROOT}/META-INF
COPY ${DEPENDENCY}/BOOT-INF/classes ${APPROOT}
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-cp","/opt/app7:/opt/app7/lib/*","-Droot.dir=/opt/app7","com.bswen.app7.Main"]
EXPOSE 8082

Pay attention to the last command in the Dockerfile:

ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-cp","/opt/app7:/opt/app7/lib/*","-Droot.dir=/opt/app7","com.bswen.app7.Main"]

The incorrect configuration was -cp /opt/app8, while the correct configuration is -cp /opt/app7. This ensures the application is installed in the correct directory.

Summary

In this post, we explored how to resolve the “Could not find or load main class” error when using Docker with Spring Boot. The key issue was an incorrect classpath in the Dockerfile, which was resolved by correcting the path from /opt/app8 to /opt/app7. Ensuring the correct classpath is crucial for the successful deployment of Spring Boot applications in Docker containers.

Final Words + More Resources

My intention with this article was to help others who might be considering solving such a problem. So I hope that’s been the case here. If you still have any questions, don’t hesitate to ask me by email: Email me

Here are also the most important links from this article along with some further resources that will help you in this scope:

Oh, and if you found these resources useful, don’t forget to support me by starring the repo on GitHub!