HttpClient java NoClassDefFoundError

时间:2017-05-22 12:55:50

标签: java json

我想从java创建一个简单的JSON客户端。 所以我构建了这段代码:

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;
    }
}

但如果我尝试启动我的应用程序,则会出现此错误:

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

我在代码附近的代码中插入了一条产生异常的注释。

  

EDIT   这是我在项目中导入的jar   enter image description here

     

编辑(我已更改.jar文件):

enter image description here

1 个答案:

答案 0 :(得分:1)

Lookup.class位于httpcore jar文件中。 确保您已添加httpcore

您似乎只添加了httpclient jar,但是您需要httpclienthttpcore来运行您的代码。

您可以从Maven存储库下载它: https://mvnrepository.com/artifact/org.apache.httpcomponents/httpcore