Silk13 Silk13 - 5 days ago 4
Java Question

Program runs fine in Eclipse, but throws error when Jar is run on the command line

Compiled with

mvn package
, I then try to run the
jar
with java
-jar app.jar
and it runs, but then throws a


NoClassDefFoundError


The program runs completely fine in Eclipse.

How do I stop this from happening so I can run the program in the command line?

Answer

Looks like you are missing dependencies. Try putting this in your pom.xml:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.5</version>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <archive>
                    <manifest>
                        <mainClass>yourpackage.YourClass</mainClass>
                    </manifest>
                </archive>
            </configuration>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

And then run: java -jar [your-package-jar-with-dependencies.jar]