Why am I getting "java.lang.NoClassDefFoundError: kong/unirest/Unirest"?

Why am I getting "java.lang.NoClassDefFoundError: kong/unirest/Unirest"?

我正在创建一个执行 POST 操作的 Java 应用程序。我需要做一个 HttpRequestWithBody 来发送一个 JSONObject。我正在使用 Unirest 来完成这项任务。在调试程序时,程序停在创建请求并分配 URL.

的行

代码如下:

HttpRequestWithBody response = Unirest.post("{{URL}}"); // This line has the problem
try {
    *** adds headers and body ***
} catch (Exception ex) {
    System.out.println(ex.getMessage());
}

这是 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>{{groupId}}</groupId>
<artifactId>program</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
    <plugins>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>Program</mainClass>
                    </manifest>
                </archive>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>10</source>
                <target>10</target>
            </configuration>
        </plugin>
    </plugins>
</build>
<dependencies>
    <dependency>
        <groupId>com.konghq</groupId>
        <artifactId>unirest-java</artifactId>
        <version>3.1.00</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.5.10</version>
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpasyncclient</artifactId>
        <version>4.1.4</version>
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpmime</artifactId>
        <version>4.5.10</version>
    </dependency>
    <dependency>
        <groupId>org.json</groupId>
        <artifactId>json</artifactId>
        <version>20190722</version>
    </dependency>
    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.8.5</version>
    </dependency>
</dependencies>

抛出的错误如下:

Exception in thread "main" java.lang.NoClassDefFoundError: kong/unirest/Unirest
at Program.main(Program.java:139)
Caused by: java.lang.ClassNotFoundException: kong.unirest.Unirest
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:583)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
... 1 more

Process finished with exit code 1

我尝试添加其他 apache.httpcomponents,例如 Core 和 Core NIO,none 这有帮助。

感谢任何帮助。

异常堆栈跟踪中的这一行意味着您的 class 路径中没有 class Unirest Exception in线程 "main" java.lang.NoClassDefFoundError: kong/unirest/Unirest

不过看起来,您在依赖项部分添加了它的库,由 maven 解析,但它在 "provided scope" 中,这意味着您期望其他东西为您提供它,例如应用程序服务器, 但我认为这不是你的环境。

那么你能否删除provided这一依赖项

<dependency>
        <groupId>com.konghq</groupId>
        <artifactId>unirest-java</artifactId>
        <version>3.1.00</version>
        <scope>provided</scope>
</dependency>

那就再试一次。

删除 <scope> 为我解决了这个问题。