0

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?

Nick Vanderhoven
  • 3,018
  • 18
  • 27
Silk13
  • 47
  • 2
  • 9
  • 2
    looks like some missing dependencies in you jar – XtremeBaumer Dec 01 '16 at 15:50
  • 1
    when you say it runs fine in eclipse, you mean as a maven project yes? – Edoardo Dec 01 '16 at 15:53
  • @eddyce Yes - Imported maven project, runs fine in eclipse but then once compiled with mvn package and I try to run in comman line throws error - Very new to maven – Silk13 Dec 01 '16 at 15:55
  • Another one. http://stackoverflow.com/questions/5797860/maven-noclassdeffounderror-in-the-main-thread – OneCricketeer Dec 01 '16 at 16:01
  • ...yes check those questions, anyway you should use the shade plugin with maven http://maven.apache.org/components/plugins/maven-shade-plugin/ – Edoardo Dec 01 '16 at 16:13

2 Answers2

3

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]

0

Hard to guess, but when you paste your pom.xml here, I am pretty sure we'll see that you have some dependency. If you do so, this dependency must be included in the classpath when running java directly.

Bastian J
  • 342
  • 2
  • 8