【问题标题】:HttpClient java NoClassDefFoundErrorHttpClient java NoClassDefFoundError
【发布时间】:2017-06-29 19:46:10
【问题描述】:

我想用 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

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

编辑 这是我在项目中导入的 jar

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

【问题讨论】:

标签: java json


【解决方案1】:

Lookup.classhttpcore jar 文件中。 确保您已添加httpcore

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-19
    • 2012-09-18
    • 2012-03-25
    • 1970-01-01
    • 2014-08-19
    • 2021-11-15
    相关资源
    最近更新 更多