Search code examples
javahttpclient

Exception in thread "main" java.lang.NoSuchFieldError: INSTANCE


I am trying to write a simple WebService Testing using HTTPClient. I have all my jars loaded up but get the above error message.

Why am I getting this error message?

Here is the code:

package HTTPClientTest;

import java.io.IOException;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.HttpClientBuilder;
import org.junit.Assert;


public class RESTTester {

    public static void main (String args[]) {

    String restURL_XML = "http://parabank.parasoft.com/parabank/services/bank/customers/12212/";


    try {

    testStatusCode(restURL_XML);

    } catch (ClientProtocolException e) {

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


public static void testStatusCode(String restURL) throws ClientProtocolException, IOException {
    HttpUriRequest request = new HttpGet(restURL);
    HttpResponse httpResponse = HttpClientBuilder.create().build().execute(request);
    Assert.assertEquals(httpResponse.getStatusLine().getStatusCode(),HttpStatus.SC_OK);
    }

}

Below is the error stacktrace,

Exception in thread "main" java.lang.NoSuchFieldError: INSTANCE
    at org.apache.http.conn.ssl.SSLConnectionSocketFactory.<clinit>(SSLConnectionSocketFactory.java:144)
    at org.apache.http.impl.client.HttpClientBuilder.build(HttpClientBuilder.java:966)
    at HTTPClientTest.RESTTester.testStatusCode(RESTTester.java:37)
    at HTTPClientTest.RESTTester.main(RESTTester.java:22)

Solution

  • I got above error because I had multiple older versions of HTTPClient in my project. I removed older versions of HTTPClient and now the error message is gone.