我有一个Maven Eclipse项目,它在Eclipse中运行良好,但是如果我尝试从命令行运行它,则无法找到依赖项。
pom.xml文件:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.something.or.rather.myapp</groupId>
<artifactId>MyApp</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>MyApp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.cloudant</groupId>
<artifactId>cloudant-client</artifactId>
<version>1.0.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
从项目目录(MyApp):
mvn eclipse:eclipse // runs fine
mvn package // runs fine
java -cp target/MyApp-1.0-SNAPSHOT.jar com.something.or.rather.myapp.MyMainClass // fails
...
Exception in thread "main" java.lang.NoClassDefFoundError: com/cloudant/client/api/CloudantClient
...
我注意到如果我将Cloudant JAR文件的完整路径放在java命令中,它只解析该异常以引发另一个异常。
java -cp /full/path/to/cloudant-client-1.0.1jar:target/MyApp-1.0-SNAPSHOT.jar com.something.or.rather.myapp.MyMainClass // still fails
...
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/http/client/methods/HttpRequestBase
...
有什么想法吗?这很痛苦。
答案 0 :(得分:2)