Jérémy
Jérémy

Reputation: 405

Adding External JAR in Maven Causes NoClassDefFoundError

Found some other questions which are about this issue, but none of them seems to have a valid solution to solve my issue.

I have an external JAR that needs to be included in my generated jar. I'm currently doing this with:

<dependency>
    <groupId>be.april.meter</groupId>
    <artifactId>JRPiCam</artifactId>
    <version>1.1.1</version>
    <scope>system</scope>
    <systemPath>${project.basedir}/src/main/resources/jrpicam-1.1.1.jar</systemPath>
</dependency>

When just running my main class within the IDE, everything works fine as it's supposed to. When however trying to run the Maven generated JAR, I'm getting a NoClassDefFoundError error. I already tried to generate a 'fat'/'shaded' jar and all that stuff, but none of it helps.

I also tried to install the jar with the Maven Install Plugin (https://eureka.ykyuen.info/2014/06/10/maven-include-system-scope-dependency-in-maven-assembly-plugin/), but this gives the exact same error as I get with my current solution.

Full pom.xml can be found here:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>be.april.meter</groupId>
    <artifactId>April-Meter</artifactId>
    <version>1.0-SNAPSHOT</version>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
            <plugin>
                <!-- Build an executable JAR -->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.0.2</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                            <mainClass>be.april.meter.Main</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.10.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.10.0</version>
        </dependency>
        <dependency>
            <groupId>be.april.meter</groupId>
            <artifactId>JRPiCam</artifactId>
            <version>1.1.1</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/src/main/resources/jrpicam-1.1.1.jar</systemPath>
        </dependency>
    </dependencies>

Thanks in advance.

-- Edit: seemed like the log4j wouldn't load either. I fixed this by adding:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>2.5.1</version>
    <executions>
        <execution>
            <id>copy-dependencies</id>
            <phase>package</phase>
            <goals>
                <goal>copy-dependencies</goal>
            </goals>
            <configuration>
                <includeScope>compile</includeScope>
                <outputDirectory>${project.build.directory}/lib/</outputDirectory>
            </configuration>
        </execution>
    </executions>
</plugin>

This causes the JRPiCam jar to be in the /lib output directory together with the log4j depdendencies, but the JRPiCam still doesn't work.

Upvotes: 1

Views: 1461

Answers (2)

J&#233;r&#233;my
J&#233;r&#233;my

Reputation: 405

I fixed this issue by uploading the jar to a private Nexus Repo. Other solutions that really 'solve' the main issue are always welcome..

Upvotes: 0

nitnamby
nitnamby

Reputation: 414

You could try maven assembly plugin as discussed. I just verified, it works with external dependencies as well. An executable jar is created when you run.

mvn clean install

 <modelVersion>4.0.0</modelVersion>
<groupId>com.spring</groupId>
<artifactId>maven-test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<properties>
    <java.version>10</java.version>
</properties>


<dependencies>
    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-all</artifactId>
        <version>1.9.5</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>2.10.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>2.10.0</version>
    </dependency>
</dependencies>
<build>
<plugins>
<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <configuration>
        <archive>
            <manifest>
                <mainClass>com.test.MyMain</mainClass>
            </manifest>
        </archive>
        <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>


    </configuration>
    <executions>
        <execution>
            <id>make-assembly</id>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>


</plugin>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.6.1</version>
        <configuration>
            <source>10</source>
            <target>10</target>
        </configuration>
    </plugin>
</plugins>
</build>

Upvotes: 1

Related Questions