My maven-spring boot project runs fine when I do so outside of Docker. But once I dockerize it I get an "Exception in thread "main" java.lang.ClassNotFoundException" when I try to run it. Searching this problem I’m told you need to make sure you have the spring-boot-maven-plugin in your plugin management and plugins inside pom.xml, and to include the main class therein. I’ve done this but the problem persists. My docker file looks like this:
FROM maven:3.8.4-openjdk-17 as builder
RUN mkdir -p /home/maven
COPY src /home/maven/
COPY chg.json /home/maven/
COPY manifest-QA.yml /home/maven/
COPY manifest-za.yml /home/maven/
COPY manifest-zb.yml /home/maven/
COPY pom.xml /home/maven/
COPY service-account.json /home/maven/
COPY sonar-project.properties /home/maven/
COPY checkLocksmith.sh /home/maven/
COPY buildConfig.json /home/maven/
COPY settings.xml /home/maven/
WORKDIR /home/maven
RUN mvn -s ./settings.xml clean package
FROM openjdk:17-oracle
EXPOSE 8080
WORKDIR /home/maven
COPY --from=builder /home/maven/target/Program-0.0.1-SNAPSHOT.jar ./app.jar
ENTRYPOINT ["java","-jar","app.jar"]
2
Answers
The problem appeared to be with my Dockerfile. This is what I changed it to
Please edit your docker file with:
FROM maven:3.8.4 AS maven
WORKDIR /home/maven
COPY . /home/maven
FROM adoptopenjdk/openjdk17:alpine-jre
ARG JAR_FILE=Program-0.0.1-SNAPSHOT.jar
WORKDIR /home/maven
COPY –from=maven /home/maven/app/target/${JAR_FILE} /app
ENTRYPOINT ["java","-jar","Program-0.0.1-SNAPSHOT.jar"]