NoClassDefFoundError语言 - 类路径中缺少依赖项



我的问题

我写了一些java代码。代码在intelllij中完美运行。
但是,当我运行它作为一个。jar文件(即,命令java -jar app.jar),我得到的错误信息:
Unable to initialize main class RSocketClient.Client
Caused by: java.lang.NoClassDefFoundError: io/rsocket/transport/ClientTransport
<标题>我的研究我一直在搜索谷歌和stackoverflow上的NoClassDefFoundError,并发现当依赖项缺失时出现错误。解决方案似乎是我需要向类路径或maven存储库添加依赖项。但是我不知道如何做到这一点(下面给出了我的pom.xml)。<标题>

我的问题我所要做的就是将我的程序转换为.jar文件,并通过我的终端(Windows10操作系统)运行它。我很感激我能得到的所有帮助。pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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>org.example</groupId>
<artifactId>RSocketSample</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<!-- Build an executable JAR -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>RSocketClient.Client</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<maven.compiler.source>16</maven.compiler.source>
<maven.compiler.target>16</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>io.rsocket</groupId>
<artifactId>rsocket-core</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>io.rsocket</groupId>
<artifactId>rsocket-transport-netty</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.32</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.32</version>
</dependency>
</dependencies>
</project>

看起来你的jar不包含RSocket本身的类,这很好。由maven创建的Jar只包含您的类(在您的模块中)。

rsocket的jar(以及您正在使用的其他jar,例如SLF4J的东西)驻留在这些第三方的维护人员准备的相应的jar中。

IntelliJ"sees"整个类路径(包括所有依赖项),并使用所有这些类路径运行应用程序。

也许你应该做的是创建一个包含依赖的jar:参见This SO thread

另一个选择是保持你的jar"原样"。但是要求它将在类路径中与所有依赖项一起运行,但它看起来不是你想要做的,这种方式用于当你开发一个库而不是一个独立的应用程序时。

最新更新