我真的很努力让我的 Maven 项目在命令行中运行。它在 Eclipse 中按照我想要的方式运行,但是在终端中使用 mvn package 命令运行我编写的单元测试,但不是我希望它运行的实际主源代码应用程序。
java -cp target/TestKata-0.0.1-SNAPSHOT.jar com.techelevator.Main
返回此错误:
Exception in thread "main" java.lang.NoClassDefFoundError: okhttp3/OkHttpClient
at com.techelevator.Main.<clinit>(Main.java:12)
Caused by: java.lang.ClassNotFoundException: okhttp3.OkHttpClient
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 1 more
我假设我收到此错误,因为依赖项未包含在 SNAPSHOT.jar 文件中,并且在到达需要依赖项的第一行后失败?非常感谢任何帮助,我对这些东西很陌生,所以如果我做了一些非常愚蠢的事情,我深表歉意。
编辑: 这是我的 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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.techelevator</groupId>
<artifactId>TestKata</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium</artifactId>
<version>2.0rc2</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-chrome-driver</artifactId>
<version>3.3.1</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.6.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.3</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehouse.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.4.0</version>
<configuration>
<mainClass>com.techelevator.Main</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
最佳答案
您必须通过 maven-assemble-plugin 将依赖项打包到 jar 中。jar-with-dependnecies
描述符可在 maven 中找到
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
您可以在运行mvn package
命令时添加执行,这将输出一个像*-with-jar-dependency.jar
这样的jar文件名,然后运行该jar文件而是通过 java
,例如:java -cp target/TestKata-0.0.1-SNAPSHOT-with-jar-dependency.jar com.techelevator.Main
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>assembly</goal>
</goals>
</execution>
</executions>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
关于java - 尝试从命令行运行 Maven 项目时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42882323/