Search code examples
javamavenpom.xmlmaven-package

Error after Maven Package


a Project of mine using Commons Chain works fine, when i run it in the IDE. Now i tried to package it with maven.

I used the following pom.xml:

<build>
    <finalName>NAME</finalName>

    <plugins>
        <plugin>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>main.Main</mainClass>
                    </manifest>
                </archive>
            </configuration>
            <version>2.6</version>
        </plugin>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
            <version>3.3</version>
        </plugin>
    </plugins>
</build>

<dependencies>
    <dependency>
        <groupId>commons-chain</groupId>
        <artifactId>commons-chain</artifactId>
        <version>1.2</version>
    </dependency>
    <dependency>
        <groupId>commons-logging</groupId>
        <artifactId>commons-logging</artifactId>
        <version>1.1.1</version>
    </dependency>
    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>18.0</version>
    </dependency>

</dependencies>

As you can see i added the Main Class to my manifest and specified the Dependencies to use.

The package process runs throught, but when i start the jar it says:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/ch
ain/impl/ChainBase
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(Unknown Source)
        at java.security.SecureClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.access$100(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at main.Main.main(Main.java:24)

Where does this error come from/what do i need to change in my config file?

Greetings,

Nicolas


Solution

  • You need to specify a classpath that has the jar files for your dependencies, something like java -cp=libs -jar main.jar where libs contains the dependency jar files. Maven will set that up for you with the maven-exec-plugin: mvn exec:java -DmainClass=main.Main. Maybe what you want is to have Maven create an "uber jar" that includes all of the class files for your dependencies so that java -jar main.jar just works. Use the maven-assembly-plugin or the maven-shade-plugin to create one. See What is an uber jar?.