1

I have trouble running the Quick Start example from Apache HttpComponents:

Initially I was using it as a guide. When that failed, I downloaded the example Java file and still with no luck. I believe there's some missing information on the page. After all a quick-start should run, and get you started.

I think this is like the problem described here.

I believe the answer is in this question (also indicated by the comment below). I've tested the suggested answer, and it works for me!

The Quick Start example code fails on the first line basically. I split it up to check things step by step.

public class QuickStart {

    public static void quickStart() throws IOException { // String[] args) throws Exception {

        CloseableHttpClient httpclient;
        httpclient = HttpClients.createDefault();

        try {
            HttpGet httpGet;
                :

This line throws an error:

  • httpclient = HttpClients.createDefault();
  • Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/http/config/Lookup

I appreciate the comment that the look-up class may be in the HttpCore module. However the quick start and other beginner tutorials do not explain HOW to bring in the Core component into my example project? There's is no mention of HttpCore or Lookup -- I did a string find on the page.

There are a couple of questions really.

  1. What is needed to fix the Quick Start example for it to run as written?
    • An implication here is that the Quick Start page is broken because the example does not start, quick or otherwise.
  2. How does one "induce" the HttpCore to integrate with a simple one Java file program illustrated by the HttpClient Quick Start?
  3. Is there or was there some intention for the HttpCore to be brought in optionally?
    • I looked in the POM file, the HttpClient has-a dependency on HttpCore for the build.
    • Do I need to add something here to bring the HttpCore along into the JAR or something?
  4. Is there a better way to use the HttpClient to make a small Java program similar to the Quick Start?
  5. More in the spirit of improving the web: Please TEST examples and Quick Starts before posting them...
    • This happens more often than folk would believe. Generally there's small things which I just fix. I think it would be better if we can post stuff that runs.

Concerning this issue. The call: HttpClients.createDefault(), should be expected to work based on the quick start code, otherwise the name "createDefault" appears to be an error. Many advance thanks ...

update:

I thought I should show what Netbeans put in the POM for the HttpComponents ...

    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.3.4</version>
        <type>jar</type>
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpcore</artifactId>
        <version>4.3.2</version>
        <type>jar</type>
    </dependency>

I've looked at some other examples. These also just call defaultClient(). So it seems like a bit more of a mystery than just there being a problem in the quick-start example. Other examples:

Still no explicit mention for Lookup or linking with HttpCore. Should these examples work? If so what 'extra' is needed in a (Netbeans) Maven build to make it happen?

Community
  • 1
  • 1
will
  • 4,799
  • 8
  • 54
  • 90
  • what do you get with `HttpClientBuilder.create();` – sschrass Jul 05 '14 at 12:20
  • 1
    I will attempt this. According to MKYong: [defaultHttpClient is Deprecated](http://www.mkyong.com/java/the-type-defaulthttpclient-is-deprecated/). So question #4, is more relevant -- Avoid posting or leaving out of date material in examples and tutorials. ~sigh~ – will Jul 05 '14 at 12:41
  • you shouldn't need more than these 2 dependencies. I would go for a CloseableHttpClient (https://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/impl/client/HttpClientBuilder.html) – sschrass Jul 05 '14 at 12:48
  • Thanks SatelliteSD and NishantShreshth the HttpClientBuilder works. And it is NOT documented for the project page. A great pity and waste of many people's time given the number of posts I found with the sample code and (_now_) broken/deprecated tutorials I saw. – will Jul 05 '14 at 12:48

1 Answers1

0

defaultHttpClient ist deprecated source, as you already found out. Use a HttpClientBuilder instead source. You will get a CloseableHttpClient, which is handy with Java 8 since you can skip some finally-blocks.

sschrass
  • 7,014
  • 6
  • 43
  • 62