71

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 :

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.

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.

Saurabh Saxena
  • 3,005
  • 10
  • 31
  • 46

9 Answers9

96

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

    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.3.4</version>
    </dependency>
Balaji Katika
  • 2,835
  • 2
  • 21
  • 19
  • 4
    And for Gradle: `compile group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.5.2'` – benscabbia May 26 '16 at 19:41
  • Hi, I'm using Maven but I can't find the solution to this issue. That's my dependency structure org.apache.httpcomponents httpclient 4.5.3 httpcore 4.4.1 It's my complete stackoverflow post https://stackoverflow.com/questions/64047804/maven-docker-java-lang-noclassdeffounderror-org-apache-http-client-methods-ht – bpdin Dec 05 '20 at 13:29
32

What could be the possible cause of this exception?

You may not have appropriate Jar in your class path.

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

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.

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

Edit:
If you are using a dependency management like Maven/Gradle (see the answer below) or SBT please use it to bring the httpclient jar for you.

Nishant
  • 54,584
  • 13
  • 112
  • 127
  • thanks for this, but now I am getting this error com.google.gwt.user.server.rpc.UnexpectedException – Saurabh Saxena Mar 12 '12 at 08:51
  • I am unsure about the new Exception, I knew little something about what causes NoClassDefFound, and I shared that with you. – Nishant Mar 12 '12 at 08:54
  • Check http://google-web-toolkit.googlecode.com/svn/javadoc/1.5/com/google/gwt/user/server/rpc/UnexpectedException.html for more details on UnexpectedException – Hardik Mishra Mar 12 '12 at 10:34
  • How do you add the JAR to the class path? I don't have any JAR for HTTPClient, it was imported within IntelliJ – vikzilla May 10 '16 at 02:18
  • you use `-cp` parameter to add Jars to the classpath. I do not use IntelliJ, but try searching "how to add jar to classpath in IntelliJ", I am sure there must be articles on it. – Nishant May 10 '16 at 09:03
4

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:

Donwload Zip file from here

A.Aleem11
  • 1,844
  • 17
  • 12
4

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.

2

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

Roshan
  • 21
  • 3
1

I have solved this problem by importing the following dependency. you must manually import httpclient

<dependency>
    <groupId>commons-httpclient</groupId>
    <artifactId>commons-httpclient</artifactId>
    <version>3.1</version>
    <exclusions>
        <exclusion>
            <groupId>commons-logging</groupId>
            <artifactId>*</artifactId>
        </exclusion>
    </exclusions>
</dependency>
Arturo Bernal
  • 31
  • 1
  • 1
1

I faced the same issue, later I found I was using wrong gradle dependency.,

Wrong:

compileOnly 'org.apache.httpcomponents:httpclient:4.5.3'

Correct:

implementation group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.5.3'

Once I changed it, it was working fine.

1
java.lang.NoClassDefFoundError on HttpAsyncClients.custom
java.lang.NoClassDefFoundError: org/apache/http/impl/nio/clien /CloseableHttpAsyncClient
java.lang.NoClassDefFoundError: org/apache/http/HttpHost 

You have to import httpclient library.

Go to maven website and download httpclient jar file. https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient

Now you have to copy this jar file into the \lib folder.

After that look for POM file and add dependencies. You can see dependencies code on maven website. Just go to end of line of code, add the following dependencies.

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

In case of Jira/Confluence, POM file location are that. (C:\Program Files\Atlassian\JIRA\atlassian-jira\META-INF\maven\com.atlassian.jira)

There is two folder at this location: atlassian-jira-webapp and jira-webapp-dist. Both folders contain POM files.

Just open one POM file in notepad++ and search for end of . enter image description here

Now go to second folder POM file and add same dependencies too. Just look into error and add dependencies.

Restart your service after that.

Alexander Pacha
  • 9,187
  • 3
  • 68
  • 108
0

After Creating Artifact Jar I was facing this Problem. What I do that, I create a new Artifact > With Module Dependency > And then Leave Blank the Main Class. In that way, after creating the artifact jar, the jar was running Ok. I, think the problem was, http-client from org.apache and from network - this two types of class used for different libraries at the same Time in my first Artifact and the problem arises. So, When I created new Artifact, and leaving unnecessary libraries and duplicate libraries, the problem gone.

Noor Hossain
  • 1,620
  • 1
  • 18
  • 25