java.lang.NoClassDefFoundError: org/apache/http/client/HttpClient
Asked Answered
O

10

76

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.

Orchid answered 12/3, 2012 at 8:43 Comment(0)
A
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.

Artamas answered 12/3, 2012 at 8:44 Comment(5)
thanks for this, but now I am getting this error com.google.gwt.user.server.rpc.UnexpectedExceptionOrchid
I am unsure about the new Exception, I knew little something about what causes NoClassDefFound, and I shared that with you.Artamas
Check google-web-toolkit.googlecode.com/svn/javadoc/1.5/com/google/… for more details on UnexpectedExceptionPuri
How do you add the JAR to the class path? I don't have any JAR for HTTPClient, it was imported within IntelliJKathaleenkatharevusa
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.Artamas
S
101

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>
Sulphanilamide answered 3/7, 2014 at 12:38 Comment(2)
And for Gradle: compile group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.5.2'Evidently
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 #64048304Bertie
A
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.

Artamas answered 12/3, 2012 at 8:44 Comment(5)
thanks for this, but now I am getting this error com.google.gwt.user.server.rpc.UnexpectedExceptionOrchid
I am unsure about the new Exception, I knew little something about what causes NoClassDefFound, and I shared that with you.Artamas
Check google-web-toolkit.googlecode.com/svn/javadoc/1.5/com/google/… for more details on UnexpectedExceptionPuri
How do you add the JAR to the class path? I don't have any JAR for HTTPClient, it was imported within IntelliJKathaleenkatharevusa
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.Artamas
M
6

I experienced a similar issue whilst using Spring Boot 3. The dependency has to be client5.

Add the following Maven dependency to your POM file.

<dependency>
  <groupId>org.apache.httpcomponents.client5</groupId>
  <artifactId>httpclient5</artifactId>
  <version>5.2.1</version>
</dependency>

https://mvnrepository.com/artifact/org.apache.httpcomponents.client5/httpclient5

Mitrailleuse answered 13/2, 2024 at 13:21 Comment(1)
I also had to add an exclusion: <dependency> <groupId>org.jboss.resteasy</groupId> <artifactId>resteasy-client</artifactId> <version>3.0.5.Final</version> <exclusions> <exclusion> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> </exclusion> </exclusions> </dependency>Menthol
W
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

Wot answered 13/1, 2016 at 18:48 Comment(2)
Didn't worked for me.. not any benefit of downloading.Sivan
Alhamdulillah, worked like a charm ! thanks for SO, @A.Aleem, I found at last one day struggling, Alhamdulillah.Omnipotent
G
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.

Grade answered 29/4, 2016 at 10:5 Comment(1)
life saver.. changed httpclient from 4.3.5 to 4.5.3 . worked fineAndrogynous
M
2

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

Mabelmabelle answered 19/9, 2017 at 13:37 Comment(0)
C
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>
Chordophone answered 26/8, 2020 at 9:50 Comment(0)
S
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.

Smitty answered 7/4, 2022 at 16:19 Comment(0)
P
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.

Preclude answered 29/7, 2022 at 10:0 Comment(0)
O
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.

Omnipotent answered 6/10, 2021 at 14:1 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.