Docker could not find or load main class in Java spring boot

Viewed 792

I have a simple spring boot server with the following structure:

.
├── Dockerfile
├── HELP.md
├── demo.iml
├── mvnw
├── mvnw.cmd
├── pom.xml
├── src
│   ├── main
│   │   ├── java
│   │   │   └── com
│   │   │       └── example
│   │   │           └── demo
│   │   │               ├── DemoApplication.java
│   │   │               ├── Employee.java
│   │   │               ├── EmployeeResource.java
│   │   │               ├── exceptions
│   │   │               │   └── UserNotFoundException.java
│   │   │               ├── repo
│   │   │               │   └── EmployeeRepo.java
│   │   │               └── service
│   │   │                   ├── DockerFile
│   │   │                   └── EmployeeService.java
│   │   └── resources
│   │       ├── META-INF
│   │       │   └── MANIFEST.MF
│   │       ├── application.properties
│   │       ├── static
│   │       └── templates
│   └── test
│       └── java
│           └── com
│               └── example
│                   └── demo
│                       └── DemoApplicationTests.java
└── target
    ├── classes
    │   ├── META-INF
    │   │   └── MANIFEST.MF
    │   ├── application.properties
    │   └── com
    │       └── example
    │           └── demo
    │               ├── DemoApplication.class
    │               ├── Employee.class
    │               ├── EmployeeResource.class
    │               ├── exceptions
    │               │   └── UserNotFoundException.class
    │               ├── repo
    │               │   └── EmployeeRepo.class
    │               └── service
    │                   └── EmployeeService.class
    ├── demo.jar
    ├── generated-sources
    │   └── annotations
    ├── generated-test-sources
    │   └── test-annotations
    └── test-classes
        └── com
            └── example
                └── demo
                    └── DemoApplicationTests.class

And my coinciding Dockerfile:

FROM "ANYONMOUS REPO" as builder
USER root
COPY src /src
ADD target/demo.jar demo.jar
EXPOSE 8080
ENTRYPOINT ["java","-jar","demo.jar"]

This server works fine when ran locally but when I create a docker image and try to run it I get the following error:

Error: Could not find or load main class com.example.demo.DemoApplication.

I am not sure how I would go about targeting the main class in the package itself, I tried using this command in my Dockerfile: CMD java -classpath src/java com.example.demo in an attempt to target this package but I still get the same error. Is there a different way to target the Jar file and the main class in Docker?

1 Answers

I faced similar issue. You could try the following;

Step 1: Add the following to your pom.xml.

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>

Step 2: Clean and build your project. This should automatically specify a value for "Main-Class" in your META-INF/MANIFEST.MF

mvn clean package spring-boot:repackage
Related