嗨,我正在使用以下 Maven 依赖项。当我在 Eclipse 中运行该应用程序时,它工作正常。但是当我将应用程序部署为 jar 文件时,它会抛出以下错误。
Caused by: java.lang.ClassNotFoundException: org.glassfish.jersey.client.JerseyClientBuilder
at java.net.URLClassLoader.findClass(Unknown Source)
以下是我的 Maven 依赖文件。
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>${jersey.client.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
<version>${jersey.client.version}</version>
</dependency>
<!-- http://mvnrepository.com/artifact/com.googlecode.json-simple/json-simple -->
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1</version>
</dependency>
</dependencies>
<properties>
<jersey.client.version>2.21</jersey.client.version>
</properties>
<dependencies>
对此有任何帮助......我浏览了以下链接,但它对我没有帮助。
running standalone java executable in eclipse results in NoClassDefFound
最佳答案
如果您只分发单个 jar(不是 fat-jar/uber-jar),您需要为其提供类路径,这是运行它所需的所有库 jar。
在你的情况下,这将是这样的:
java -jar my.jar -cp $HOME/.m2/repository/org/glassfish/jersey/core/jersey-client/2.21/jersey-client-2.21.jar
在 :
之后,您需要添加您拥有的所有其他依赖项。
另一种选择是使用例如用于构建 uber jar 的程序集插件(包含所有其他 jar、库和代码的 jar):http://maven.apache.org/plugins/maven-assembly-plugin/usage.html :
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.6</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
然后使用:mvn clean package assembly:single
构建jar,看到现在target
中有两个jar,较大的一个是uber jar您可以分发。
关于java.lang.ClassNotFoundException : org. glassfish.jersey.client.JerseyClientBuilder,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39198911/