Issue
I have recently dockerized a maven / springboot project. It runs outside docker just fine, and it even starts in a docker container, but as soon as I try to interact with the application, I receive a java.lang.ClassNotFoundException
.
My understanding is that this has to do with the classpath, but why would the jar run locally and not in the docker container?
The class that is missing belongs to an external jar file that is in the project directory structure and listed in the pom.xml as a dependency:
<dependency>
<groupId>externalJar</groupId>
<artifactId>externalJar</artifactId>
<version>0.0.1</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/library/externalJar.jar</systemPath>
</dependency>
Here is my dockerfile:
FROM openjdk:8-alpine
RUN apk update && apk add bash
RUN mkdir -p /opt/app
ENV PROJECT_HOME /opt/app
COPY target/main.jar $PROJECT_HOME/main.jar
WORKDIR $PROJECT_HOME
EXPOSE 8282
CMD ["java", "-Djava.security.egd=file:/dev/./urandom","-jar","./main.jar"]
What am I missing?
Solution
I have run into the same problem. In an app we include some old libraries that only exist as external jars and locally it could run, but on a docker image it could not.
The trick was to include the <includeSystemScope>true</includeSystemScope>
besides everything else.
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>
</plugins>
</build>
so the plugin dependency is something like below, but was not enough until the includeSystemScope
was added.
<dependency>
<groupId>dddd</groupId>
<artifactId>ggggg</artifactId>
<version>1.2.1</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/aaa.jar</systemPath>
</dependency>
found it from this link
https://programmer.group/5dd6e1dae23a9.html
Answered By - thahgr
Answer Checked By - Dawn Plyler (JavaFixing Volunteer)