程序在Eclipse中运行正常,但在命令行上运行Jar时抛出错误

时间:2016-12-01 15:49:47

标签: java maven

使用mvn package编译,然后尝试使用java jar运行-jar app.jar并运行,但随后抛出

  

NoClassDefFoundError的

该程序在Eclipse中运行完全正常。

如何阻止这种情况发生,以便我可以在命令行中运行程序?

1 个答案:

答案 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]