java NoClassDefFoundError: HttpClient 4 (APACHE)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2903132/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 23:21:41  来源:igfitidea点击:

NoClassDefFoundError: HttpClient 4 (APACHE)

javaapache-commonsapache-felix

提问by Pujan Srivastava

I am using apache http commons 4.I have added both httpcore-4.0.1.jar and httpclient-4.0.1.jar in the classpath of netbeans. I am getting error:
java.lang.NoClassDefFoundError: org/apache/http/impl/client/DefaultHttpClient

我正在使用 apache http commons 4.我在 netbeans 的类路径中添加了 httpcore-4.0.1.jar 和 httpclient-4.0.1.jar。我收到错误:
java.lang.NoClassDefFoundError: org/apache/http/impl/client/DefaultHttpClient

My Code is as follows. Please help.

我的代码如下。请帮忙。

import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;

public class HttpClientManager {
    public HttpClient httpclient;
    public HttpClientManager() {
        this.init();
    }

    public void init() {
        try {
            httpclient = new DefaultHttpClient();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void getCourseList() {
        String url = "http://exnet.in.th/api.php?username=demoinst&ha=2b62560&type=instructor";
        HttpGet httpget = new HttpGet(url);

        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        try {
            String responseBody = httpclient.execute(httpget, responseHandler);
            System.out.println(responseBody);

        } catch (Exception e) {
        }    
    }
}

采纳答案by Pujan Srivastava

This exception happened because of Felix did not find the actual class as it was not included the import. Thanks all for answering this.

发生此异常是因为 Felix 没有找到实际的类,因为它没有包含在导入中。感谢大家回答这个问题。

OK I got it work by adding path of DefaultHttpClient to felix.properties –

好的,我通过将 DefaultHttpClient 的路径添加到 felix.properties 使其工作 -

回答by Adeel Ansari

Just to point out NoClassDefFoundErrorand NoClassFoundExceptionare not the one and the same thing?

只是要指出NoClassDefFoundErrorNoClassFoundException是不是一回事?

You might want to look into Demystified class loading problem series.

您可能想查看Demystified 类加载问题系列

All the best.

一切顺利。

回答by stacker

httpclient-4.0.1.jarcontains org.apache.http.impl.client.DefaultHttpClientcheck your classpath for typos.

httpclient-4.0.1.jar包含org.apache.http.impl.client.DefaultHttpClient检查您的类路径是否有拼写错误。