HTTPClient Example - Exception in thread “main” java.lang.NoSuchFieldError: INSTANCE

你。 提交于 2019-11-27 02:03:59

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.

Tim

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!