2

I'm creating a Java application that does a POST action. I need to do a HttpRequestWithBody to send a JSONObject. I'm using Unirest to accomplish this task. While debugging the program stops at the line that creates the request and asigns the URL.

Here is the code:

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

And this is the 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>

The error thrown is the following:

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

I tried adding other apache.httpcomponents such as Core and Core NIO, none of this helped.

Any help appreciated thanks.

Olaf Kock
  • 46,930
  • 8
  • 59
  • 90
ParticleDuality
  • 139
  • 3
  • 13

2 Answers2

4

This line in exception's stack trace means that you don't have that class Unirest in your class path Exception in thread "main" java.lang.NoClassDefFoundError: kong/unirest/Unirest

However it seems, you added it's lib in dependencies section to be resolved by maven, but it's in "provided scope", and that means you're expecting something else to provide it to you like application server for instance, but i think it's not your environment.

So could you remove that line provided for this dependency

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

Then give it another try.

0

removing the <scope> solved it for me.

Mahorad
  • 1,258
  • 1
  • 15
  • 22