user3599828
user3599828

Reputation: 331

Adding HttpClient jar to eclipse

I added the Apache HttpClient 4.3.5 jar to a new user library in my project, but whenever I run my program I receive NoClassDefFound runtime error. I can tell this is caused specifically by the HttpClient classes. I don't know how to alleviate this issue.

Upvotes: 1

Views: 3207

Answers (1)

Paul Samsotha
Paul Samsotha

Reputation: 209012

NoClassDefFound usually means you're missing required dependencies on the class classpath.

If you take a look at the Maven pom.xml for httpclient, you will see it has some transitive dependencies, meanings it depends on other artifacts.

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpcore</artifactId>
</dependency>
<dependency>
    <groupId>commons-logging</groupId>
    <artifactId>commons-logging</artifactId>
</dependency>
<dependency>
    <groupId>commons-codec</groupId>
    <artifactId>commons-codec</artifactId>
</dependency>

So you can see three artifacts httpclient is dependent on: httpcore, commons-logging, and commons-codec.

That being said, you may being missing these dependencies (hence the NoClassDefFound). If you are using Maven, when you add httpclient as a dependency, Maven will pull these transitive dependencies int for you.

However, it doesn't look like you are using Maven. So what you'll want to do is download the entire package at the HttpComponents Home Page. If you grab the binary distrubutions, like 4.3.5.zip, and unzip it, you will will see all these jars in the lib dir:

commons-codec-1.6
commons-logging-1.1.3
fluent-hc-4.3.5
httpclient-4.3.5
httpclient-cache-4.3.5
httpcore-4.3.2
httpmime-4.3.5

Best thing to do is just add all those jars into one library. Then add all that library to your project.

  1. Simple go to [Window] → [Preferences] → [Java] → [Build Path] → [User Libraries]
  2. Select New and type in a name
  3. Select the new Library, Select Add External Jars, browse and add all the jars in the lib dir that you downloaded.
  4. Add the library to your project.

Upvotes: 1

Related Questions