0

When I run the following code:

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
    public static void main(String[] args) {
        HttpClient client = HttpClientBuilder.create().build();
        HttpGet request = new HttpGet("https://www.google.com/?q=java");
        try {
            HttpResponse response = client.execute(request);
            System.out.println(response.getStatusLine());

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

I get the following error:

Exception in thread "main" java.lang.NoSuchFieldError: INSTANCE at org.apache.http.conn.ssl.SSLConnectionSocketFactory.(SSLConnectionSocketFactory.java:144) at org.apache.http.impl.client.HttpClientBuilder.build(HttpClientBuilder.java:966) at HttpExample.main(HttpExample.java:12)

halfer
  • 19,824
  • 17
  • 99
  • 186
  • What is the version o Apache HttpCore? – Centos Nov 17 '18 at 08:25
  • httpcore-4.4.10.jar – Mark Burrow Nov 17 '18 at 08:28
  • Did you try to use new DefaultHttpClient() to initialize HttpClient: HttpClient client = new DefaultHttpClient(); – Centos Nov 17 '18 at 08:30
  • Yeah, that is working. But why is this not working? – Mark Burrow Nov 17 '18 at 08:32
  • Maybe you have two different version of same class in your classpath as it is described in this answer https://stackoverflow.com/questions/22330848/httpclient-example-exception-in-thread-main-java-lang-nosuchfielderror-inst – Centos Nov 17 '18 at 08:37
  • Please read [Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers?](//meta.stackoverflow.com/q/326569) - the summary is that this is not an ideal way to address volunteers, and is probably counterproductive to obtaining answers. Please refrain from adding this to your questions. – halfer Nov 17 '18 at 08:58
  • Note that duplicate votes are _good_ things here - they signpost question authors to a ready-made solution. Please do not ask readers not to vote as duplicate - such requests should be ignored. – halfer Nov 17 '18 at 09:00

0 Answers0