使用mvn package
编译,然后尝试使用java jar
运行-jar app.jar
并运行,但随后抛出
NoClassDefFoundError的
该程序在Eclipse中运行完全正常。
如何阻止这种情况发生,以便我可以在命令行中运行程序?
答案 0 :(得分:3)
看起来你缺少了依赖项。试着把它放在你的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>
然后运行: java -jar [your-package-jar-with-dependencies.jar]