簡體   English   中英

NoClassDefFoundError - 類路徑中缺少依賴項

[英]NoClassDefFoundError - missing dependencies in the classpath

我的問題

我寫了一些java代碼。 代碼在 intellij 中完美運行。
但是當我將它作為 .jar 文件(即命令java -jar app.jar )運行時,我收到錯誤消息:
<?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>

我的研究

我一直在 google 和 stackoverflow 上搜索 NoClassDefFoundError 並發現當缺少依賴項時會出現錯誤 解決方案似乎是我需要向 classpath 或 maven repository 添加依賴項 但我不知道該怎么做(我的 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“看到”整個類路徑(包括所有依賴項)並使用所有這些類路徑運行您的應用程序。

可能您應該做的是創建一個包含依賴項的 jar:請參閱此 SO 線程

另一種選擇是保持你的 jar “原樣”,但要求它將與類路徑中的所有依賴項一起運行,但它看起來不是你想要做的,這種方式在你開發庫時使用而不是獨立的應用程序。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM