我正在学习 maven,但在导入依赖项时遇到问题,以下是我的 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.myapp.app</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>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>3.3</version>
</dependency>
</dependencies>
</project>
以下是我的java文件
import org.apache.commons.net.ftp.FTPClient;
public class App
{
public static void main( String[] args )
{
System.out.println( "Hello World!" );
FTPClient ftp = new FTPClient();
}
}
我使用mvn compile 编译并使用mvn package创建了 jar,两者都执行没有任何错误。但是当我使用命令运行应用程序时
java -cp target/myapp-1.0-SNAPSHOT.jar com.myapp.app.App
它显示以下错误
Hello World!
Exception in thread "main" java.lang.NoClassDefFoundError:
org/apache/commons/net/ftp/FTPClient
at com.myapp.app.App.main(App.java:16)
Caused by: java.lang.ClassNotFoundException:
org.apache.commons.net.ftp.FTPClient
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 1 more