I am using HttpClient components from Apache for the following simple program and I see the below exception:
Exception in thread "main" java.lang.NoSuchFieldError: INSTANCE
    at org.apache.http.impl.io.DefaultHttpRequestWriterFactory.(DefaultHttpRequestWriterFactory.java:52)
    at org.apache.http.impl.io.DefaultHttpRequestWriterFactory.(DefaultHttpRequestWriterFactory.java:56)
    at org.apache.http.impl.io.DefaultHttpRequestWriterFactory.(DefaultHttpRequestWriterFactory.java:46)
    at org.apache.http.impl.conn.ManagedHttpClientConnectionFactory.(ManagedHttpClientConnectionFactory.java:72)
    at org.apache.http.impl.conn.ManagedHttpClientConnectionFactory.(ManagedHttpClientConnectionFactory.java:84)
    at org.apache.http.impl.conn.ManagedHttpClientConnectionFactory.(ManagedHttpClientConnectionFactory.java:59)
    at org.apache.http.impl.conn.PoolingHttpClientConnectionManager$InternalConnectionFactory.(PoolingHttpClientConnectionManager.java:487)
    at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.(PoolingHttpClientConnectionManager.java:147)
    at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.(PoolingHttpClientConnectionManager.java:136)
    at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.(PoolingHttpClientConnectionManager.java:112)
    at org.apache.http.impl.client.HttpClientBuilder.build(HttpClientBuilder.java:726)
    at com.starwood.rms.controller.property.HttpExample.main(HttpExample.java:14)
public class HttpExample {
    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 am using
- Httpclient-4.3.3.jar
 - Httpcore-4.3.2.jar
 
Any ideas?
This code works...without any error.. check the packages if you are using similar import .
package com.jai.http;
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 class HttpExample {
    /**
     * @param args
     */
    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();
        }
    }
}
    Looking at the source code of DefaultHttpRequestWriterFactory
package org.apache.http.impl.io;
import org.apache.http.HttpRequest;
import org.apache.http.annotation.Immutable;
import org.apache.http.io.HttpMessageWriter;
import org.apache.http.io.HttpMessageWriterFactory;
import org.apache.http.io.SessionOutputBuffer;
import org.apache.http.message.BasicLineFormatter;
import org.apache.http.message.LineFormatter;
@Immutable
public class  [More ...] DefaultHttpRequestWriterFactory implements HttpMessageWriterFactory<HttpRequest> {
    public static final DefaultHttpRequestWriterFactory INSTANCE = new DefaultHttpRequestWriterFactory();
    private final LineFormatter lineFormatter;
    public  [More ...] DefaultHttpRequestWriterFactory(final LineFormatter lineFormatter) {
        super();
        this.lineFormatter = lineFormatter != null ? lineFormatter : BasicLineFormatter.INSTANCE;
    }
    public  [More ...] DefaultHttpRequestWriterFactory() {
        this(null);
    }
    public HttpMessageWriter<HttpRequest>  [More ...] create(final SessionOutputBuffer buffer) {
        return new DefaultHttpRequestWriter(buffer, lineFormatter);
    }
}
Are you sure you are using HttpCore 4.3.2? DefaultHttpRequestWriterFactory try to resolve
BasicLineFormatter.INSTANCE
field but can not find it.
Check your classpath for libraries which could contains another BasicLineFormatter class, maybe you have a HttpCore from an old version in conflict with the 4.3.2 version.
I had this problem with Hadoop. It used an old version of httpclient-4.2.5.jar and httpcore-4.2.5.jar in their shared lib.
I solved this by shading parts via the maven-shade-plugin
<relocations>
    <relocation>
        <pattern>org.apache.http</pattern>
        <shadedPattern>shaded.org.apache.http</shadedPattern>
    </relocation>
</relocations>
    Caused by: java.lang.NoSuchFieldError: INSTANCE
one of the solution of java.lang.NoSuchFieldError: INSTANCE : This happens if we have two diff version of same class in our classpath…. […], So we first find that class(one version of class) , click that class, select "build path", then we click "remove from build path" . by 333ccc333
I had this problem. It looks like there is a problem while initializing HttpClient with HttpClientBuilder.create().build(). If you want more immediate solution just use new DefaultHttpClient() to initialize HttpClient.
HttpClient client = new DefaultHttpClient();
    For those using Webpshere, make sure your class loading policy is set to "Parent Last", otherwise it will not work since WAS is using its own version of commons http which can be conflicting.
I had this problem too, i realized it was when we upgraded to java 1.8, i just downgraded to 1.7 and works as expected. Not sure why the version became an issue.
I also was frustrated by this and Eclipse until I realized that similar to Pat B's Webpshere tip, it does cause issues for Eclipse if you have the dependencies in the wrong order.
Properties -> Java Build Path -> Order and Export
Play a bit around here with the order of core and client.
来源:https://stackoverflow.com/questions/22330848/httpclient-example-exception-in-thread-main-java-lang-nosuchfielderror-inst