user3286012
user3286012

Reputation: 141

Exception in thread "main" java.lang.NoClassDefFoundError on HttpAsyncClients.custom

Hey Guys Please help me out and I'm new to java.

I'm trying to run an async HTTP request code that I've copied in the net to try. It appears I have all the necessary jar files below and compiled using Eclipse.

httpcore-4.2.3 httpasyncclient-4.1-beta1 httpclient-4.3-beta1 httpcore-nio-4.4

However, at run time, I'm getting the following error. The error is pointing to the use of HttpAsyncClients.custom which appears to be in order. I've tried even using HttpAsyncClients.createdefault but the same issue persist. Kindly help please. Thanks.

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/http/config/Lookup
    at org.apache.http.impl.nio.client.HttpAsyncClients.custom(HttpAsyncClients.java:54)
    at AsyncHTTP.main(AsyncHTTP.java:23)
Caused by: java.lang.ClassNotFoundException: org.apache.http.config.Lookup
    at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 2 more

Here is the code I'm trying to run

import java.util.concurrent.CountDownLatch;

import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.concurrent.FutureCallback;
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.apache.http.impl.nio.client.HttpAsyncClients;

/**
 * This example demonstrates a fully asynchronous execution of multiple HTTP exchanges
 * where the result of an individual operation is reported using a callback interface.
 */
public class AsyncHTTP {

    public static void main(final String[] args) throws Exception {
        //RequestConfig requestConfig = RequestConfig.custom()
        //    .setSocketTimeout(3000)
        //    .setConnectTimeout(3000).build();
        CloseableHttpAsyncClient httpclient = HttpAsyncClients.custom()
                .setMaxConnTotal(100)
                .setMaxConnPerRoute(100)
                .build();
        try {
            httpclient.start();
            final HttpGet[] requests = new HttpGet[] {
                    new HttpGet("http://www.apache.org/"),
                    new HttpGet("https://www.verisign.com/"),
                    new HttpGet("http://www.google.com/")
            };
            final CountDownLatch latch = new CountDownLatch(requests.length);
            for (final HttpGet request: requests) {
                httpclient.execute(request, new FutureCallback<HttpResponse>() {

                    public void completed(final HttpResponse response) {
                        latch.countDown();
                        System.out.println(request.getRequestLine() + "->" + response.getStatusLine());
                    }

                    public void failed(final Exception ex) {
                        latch.countDown();
                        System.out.println(request.getRequestLine() + "->" + ex);
                    }

                    public void cancelled() {
                        latch.countDown();
                        System.out.println(request.getRequestLine() + " cancelled");
                    }

                });
            }
            latch.await();
            System.out.println("Shutting down");
        } finally {
            httpclient.close();
        }
        System.out.println("Done");
    }

}

Upvotes: 1

Views: 9636

Answers (4)

Moin Abbas Qureshi
Moin Abbas Qureshi

Reputation: 26

java.lang.NoClassDefFoundError on HttpAsyncClients.custom

java.lang.NoClassDefFoundError: org/apache/http/impl/nio/clien /CloseableHttpAsyncClient

java.lang.NoClassDefFoundError: org/apache/http/HttpHost 

You have to import the http-nio core library.

Go to the maven website and download httpcore-nio jar file. (https://mvnrepository.com/artifact/org.apache.httpcomponents/httpcore-nio)

Now you have to copy this jar file in \lib folder. In case of Jira or confluence location is this one (https://mvnrepository.com/artifact/org.apache.httpcomponents/httpcore-nio )

After that look for POM file and add dependencies. You can see dependencies code on maven website. Just go to end of line of code, add following dependencies.

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpcore-nio</artifactId>
    <version>4.4.15</version>
</dependency>

In case of Jira/Confluence, POM file location are that. (C:\Program Files\Atlassian\JIRA\atlassian-jira\META-INF\maven\com.atlassian.jira)

There is two folder at this location : atlassian-jira-webapp and jira-webapp-dist . Both folder contains POM files.

Just open one POM file in notepad++ and search for end of . enter image description here

Now go to second folder POM file and add same dependencies too. Just look into error and add dependencies.

Restart service after that.

Upvotes: 0

Danillo
Danillo

Reputation: 21

I managed to solve this problem in my project adding the Apache HttpCore class "4.3-alpha1.

<dependency>
<groupId> org.apache.httpcomponents </ groupId>
<artifactId> httpcore </ artifactId>
<version> 4.3-alpha1 </ version>
</ dependency>

Good luck.

Upvotes: 2

user3286012
user3286012

Reputation: 141

I've resolved thing by adding the latest jar of each of the previous jars I'm referencing.

Upvotes: 0

Reenactor Rob
Reenactor Rob

Reputation: 1526

When a NoClassDefFoundError occurs during runtime, it means that you didn't supply a reference to the dependency either when building the final .war/.ear, or in the environment classpath.

Use winzip or the jar command to look at the files in your archive and see if the needed .jar file is present.

If not, put the JAR-file on the classpath or in the export library list of the project in Eclipse. Project->Properties->Java Build Path. Do this and the problem will go away.

You should find a ton of google links to help you with this.

Upvotes: 0

Related Questions