. lang。NoClassDefFoundError:org/apache/http/client/HttpClient

时间:2022-10-29 15:38:55

I am trying to make a get request from the GWT servlet to get JSON response from a web service. Following is the code in my servlet :

我正在尝试从GWT servlet发出get请求,以从web服务获得JSON响应。下面是我的servlet中的代码:

public String getQueData() throws IllegalArgumentException {
    String message = null;
    try {           
        HttpClient httpclient = new DefaultHttpClient(); 
        JSONParser parser = new JSONParser();

        String url = "working - url";
        HttpResponse response = null;
        response = httpclient.execute(new HttpGet(url));

        JSONObject json_data = null;
        json_data = (JSONObject)parser.parse(EntityUtils.toString(response.getEntity()));
        JSONArray results = (JSONArray)json_data.get("result");
        for (Object queid : results) {
            message = message.concat((String) ((JSONObject)queid).get("id"));
            message = message.concat("\t");
            message = message.concat((String) ((JSONObject)queid).get("owner"));
            message = message.concat("\n");
        }
      } catch (Exception e) {
    message = e.toString();
    }
    return message;
}

Getting the following exception on trying to make get request from a GWT servlet.

在尝试从GWT servlet发出get请求时获得以下异常。

java.lang.NoClassDefFoundError: org/apache/http/client/HttpClient
    at java.lang.Class.getDeclaredConstructors0(Native Method)
    at java.lang.Class.privateGetDeclaredConstructors(Class.java:2389)
    at java.lang.Class.getConstructor0(Class.java:2699)
    at java.lang.Class.newInstance0(Class.java:326)
    at java.lang.Class.newInstance(Class.java:308)
    at org.mortbay.jetty.servlet.Holder.newInstance(Holder.java:153)
    at org.mortbay.jetty.servlet.ServletHolder.initServlet(ServletHolder.java:428)
    at org.mortbay.jetty.servlet.ServletHolder.getServlet(ServletHolder.java:339)
    at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:487)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1166)
    at com.google.appengine.tools.development.HeaderVerificationFilter.doFilter(HeaderVerificationFilter.java:35)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at com.google.appengine.api.blobstore.dev.ServeBlobFilter.doFilter(ServeBlobFilter.java:60)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at com.google.apphosting.utils.servlet.TransactionCleanupFilter.doFilter(TransactionCleanupFilter.java:43)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at com.google.appengine.tools.development.StaticFileFilter.doFilter(StaticFileFilter.java:122)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at com.google.appengine.tools.development.BackendServersFilter.doFilter(BackendServersFilter.java:97)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:388)
    at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
    at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
    at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765)
    at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418)
    at com.google.appengine.tools.development.DevAppEngineWebAppContext.handle(DevAppEngineWebAppContext.java:78)
    at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
    at com.google.appengine.tools.development.JettyContainerService$ApiProxyHandler.handle(JettyContainerService.java:362)
    at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
    at org.mortbay.jetty.Server.handle(Server.java:326)
    at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)
    at org.mortbay.jetty.HttpConnection$RequestHandler.content(HttpConnection.java:938)
    at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:755)
    at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:218)
    at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
    at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:409)
    at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582)
Caused by: java.lang.ClassNotFoundException: org.apache.http.client.HttpClient
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at com.google.appengine.tools.development.IsolatedAppClassLoader.loadClass(IsolatedAppClassLoader.java:176)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)

What could be the possible cause of this exception? How it could be removed?

这种例外的可能原因是什么?如何将其移除?

I am using jdk1.6.0_30 on ubuntu 10.04.

我在ubuntu 10.04上使用jdk1.6.0_30。

5 个解决方案

#1


24  

What could be the possible cause of this exception?

这种例外的可能原因是什么?

You may not have appropriate Jar in your class path.

在类路径中可能没有合适的Jar。

How it could be removed?

如何将其移除?

By putting HTTPClient jar in your class path. If it's a webapp, copy Jar into WEB-INF/lib if it's standalone, make sure you have this jar in class path or explicitly set using -cp option

通过将HTTPClient jar放到类路径中。如果是webapp,将Jar拷贝到WEB-INF/lib中(如果是独立的),确保在类路径中有这个Jar,或者使用-cp选项显式地设置这个Jar

as the doc says,

就像医生说的那样,

Thrown if the Java Virtual Machine or a ClassLoader instance tries to load in the definition of a class (as part of a normal method call or as part of creating a new instance using the new expression) and no definition of the class could be found.

如果Java虚拟机或类加载器实例试图装入类的定义(作为普通方法调用的一部分或使用新表达式创建新实例的一部分),则无法找到类的定义,则抛出。

The searched-for class definition existed when the currently executing class was compiled, but the definition can no longer be found.

当当前执行的类被编译时,搜索类定义就存在了,但是这个定义再也找不到了。

#2


45  

If its a maven project, add the below dependency in your pom file

如果是maven项目,请在pom文件中添加以下依赖项

    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.3.4</version>
    </dependency>

#3


2  

I solved this issue for myself, I found there's was two files of http-client with different version of other dependent jar files. So there may version were collapsing between libraries files so remove all old/previous libraries files and re-add are jar files from lib folder of this zip file:

我自己解决了这个问题,我发现有两个http客户端文件,不同版本的其他依赖jar文件。所以可能有版本在库文件之间崩溃,所以从这个zip文件的lib文件夹中删除所有旧的/以前的库文件并重新添加jar文件:

Donwload Zip file from here

从这里加载Zip文件

#4


2  

I was facing the same issue. In my case, I had a dependency of httpclient with an older version while sendgrid required a newer version of httpclient. Just make sure that the version of httpclient is correct in your dependencies and it would work fine.

我也面临着同样的问题。在我的例子中,httpclient与旧版本的依赖关系,而sendgrid需要更新的httpclient版本。只要确保httpclient的版本在您的依赖项中是正确的,它就可以正常工作。

#5


1  

This problem occurs if there are different jar versions. Especially versions of httpcore and httpclient. Use same versions of httpcore and httpclient.

如果有不同的jar版本,就会出现这个问题。特别是httpcore和httpclient的版本。使用相同版本的httpcore和httpclient。

#1


24  

What could be the possible cause of this exception?

这种例外的可能原因是什么?

You may not have appropriate Jar in your class path.

在类路径中可能没有合适的Jar。

How it could be removed?

如何将其移除?

By putting HTTPClient jar in your class path. If it's a webapp, copy Jar into WEB-INF/lib if it's standalone, make sure you have this jar in class path or explicitly set using -cp option

通过将HTTPClient jar放到类路径中。如果是webapp,将Jar拷贝到WEB-INF/lib中(如果是独立的),确保在类路径中有这个Jar,或者使用-cp选项显式地设置这个Jar

as the doc says,

就像医生说的那样,

Thrown if the Java Virtual Machine or a ClassLoader instance tries to load in the definition of a class (as part of a normal method call or as part of creating a new instance using the new expression) and no definition of the class could be found.

如果Java虚拟机或类加载器实例试图装入类的定义(作为普通方法调用的一部分或使用新表达式创建新实例的一部分),则无法找到类的定义,则抛出。

The searched-for class definition existed when the currently executing class was compiled, but the definition can no longer be found.

当当前执行的类被编译时,搜索类定义就存在了,但是这个定义再也找不到了。

#2


45  

If its a maven project, add the below dependency in your pom file

如果是maven项目,请在pom文件中添加以下依赖项

    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.3.4</version>
    </dependency>

#3


2  

I solved this issue for myself, I found there's was two files of http-client with different version of other dependent jar files. So there may version were collapsing between libraries files so remove all old/previous libraries files and re-add are jar files from lib folder of this zip file:

我自己解决了这个问题,我发现有两个http客户端文件,不同版本的其他依赖jar文件。所以可能有版本在库文件之间崩溃,所以从这个zip文件的lib文件夹中删除所有旧的/以前的库文件并重新添加jar文件:

Donwload Zip file from here

从这里加载Zip文件

#4


2  

I was facing the same issue. In my case, I had a dependency of httpclient with an older version while sendgrid required a newer version of httpclient. Just make sure that the version of httpclient is correct in your dependencies and it would work fine.

我也面临着同样的问题。在我的例子中,httpclient与旧版本的依赖关系,而sendgrid需要更新的httpclient版本。只要确保httpclient的版本在您的依赖项中是正确的,它就可以正常工作。

#5


1  

This problem occurs if there are different jar versions. Especially versions of httpcore and httpclient. Use same versions of httpcore and httpclient.

如果有不同的jar版本,就会出现这个问题。特别是httpcore和httpclient的版本。使用相同版本的httpcore和httpclient。