bircastri
bircastri

Reputation: 2167

HttpClient java NoClassDefFoundError

I want to make a simple JSON client from java. So I have build this code:

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.json.JSONException;
import org.json.JSONObject;

public class JsonSimple {
    private static final String FILENAME = "C:\\Users\\a41g.txt";

    public static void main(String[] args) {
        JsonSimple s = new JsonSimple();
        s.http();
    }

    public HttpResponse http() {
        BufferedReader br = null;
        FileReader fr = null;


        try {
            fr = new FileReader(FILENAME);
        } catch (FileNotFoundException e1) {

        }

        br = new BufferedReader(fr);

        String sCurrentLine;

        try {
            br = new BufferedReader(new FileReader(FILENAME));
        } catch (FileNotFoundException e1) {

        }

        try {
            while ((sCurrentLine = br.readLine()) != null) {
                System.out.println(sCurrentLine);
                try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) { //THIS LINE GENERATED EXCEPTION
                    HttpPost request = new HttpPost("http://mylik");

                    try {
                        JSONObject json = new JSONObject(sCurrentLine);
                        StringEntity se = new StringEntity(json.toString());
                        request.addHeader("content-type", "application/json");
                        request.setEntity(se);
                        HttpResponse response = httpClient.execute(request);
                        if (response != null) {
                            InputStream in = response.getEntity().getContent(); //Get the data in the entity
                        }
                    } catch (JSONException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }



        return null;
    }
}

but if I try to start my application I have this error:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/http/config/Lookup
    at JsonSimple.http(JsonSimple.java:47)
    at JsonSimple.main(JsonSimple.java:20)
Caused by: java.lang.ClassNotFoundException: org.apache.http.config.Lookup
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    ... 2 more

I have insert a comment in my code near the line code that generate the exception.

EDIT this is the jar that I have imported in my project enter image description here

EDIT ( I have change .jar file) in this:

enter image description here

Upvotes: 0

Views: 9929

Answers (1)

Bruno Brs
Bruno Brs

Reputation: 693

Lookup.class is inside the httpcore jar file. Make sure you have added httpcore.

It seems you have added just httpclient jar, but you'd need both httpclient and httpcore to run your code.

You can download it from the Maven repository: https://mvnrepository.com/artifact/org.apache.httpcomponents/httpcore

Upvotes: 2

Related Questions