1

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?

Jeff
  • 4,285
  • 15
  • 63
  • 115
  • I think your issue is because of `classpath` make sure you add `classpath` and it should work – Muhammad Soliman Jun 05 '18 at 14:53
  • I'm somewhat new to maven. Where is this added? in the pom or in a docker file somewhere? Additionally, why would it run outside a docker file but not inside one? – Jeff Jun 05 '18 at 14:55
  • https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#System_Dependencies If you want to include the externalJar inside your spring boot jar, you will have to import it in as a resource. – Compass Jun 05 '18 at 14:57
  • I saw that article. I believe I've done that above? – Jeff Jun 05 '18 at 14:58
  • 1
    https://stackoverflow.com/a/20795547/1334561 – Muhammad Soliman Jun 05 '18 at 14:59
  • need your dockerfile to find out your problem. add your dockerfile in question. – GolamMazid Sajib Jun 05 '18 at 15:00
  • @msoliman I've now done that, and I'm printing out the classpath in my Application.main(), and when running on the docker instance, it's only printing out my jar for the classpath. That should be the case, right since that other jar should be included inside my jar? – Jeff Jun 05 '18 at 15:12
  • you can use `-cp` to include directory for the other `jar` file – Muhammad Soliman Jun 05 '18 at 15:18
  • I was able to reproduce the error outside of docker. using `java -jar ./main.jar` gives the error now. adding `-cp ./src/main/resources/externalJar.jar` didn't help – Jeff Jun 05 '18 at 15:32
  • should be `-cp ./src/main/resources/` – Muhammad Soliman Jun 05 '18 at 15:43
  • Springboot should build a self contained executable jar, yes? Did you do `mvn package spring-boot:repackage` to build it? See https://docs.spring.io/spring-boot/docs/current/reference/html/build-tool-plugins-maven-plugin.html – K.Nicholas Jun 05 '18 at 17:38
  • I ended up creating a local repo in the project, and that fixed the issue. – Jeff Jun 05 '18 at 19:04

1 Answers1

4

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

thahgr
  • 718
  • 1
  • 10
  • 27