HTTPClient示例 - 线程“main”中的异常java.lang.NoSuchFieldError:INSTANCE

时间:2014-03-11 16:33:17

标签: java apache-httpclient-4.x

我正在使用Apache的HttpClient组件来完成以下简单程序,我看到以下异常:

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();
        }
    }
}

我正在使用

  • 的HttpClient-4.3.3.jar
  • 的HttpCore-4.3.2.jar

有什么想法吗?

9 个答案:

答案 0 :(得分:6)

查看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);
    }

}

您确定使用的是HttpCore 4.3.2吗? DefaultHttpRequestWriterFactory尝试解决

BasicLineFormatter.INSTANCE

字段但找不到它。

检查您的类路径中是否包含可能包含另一个BasicLineFormatter类的库,可能您的旧版本的HttpCore与4.3.2版本冲突。

答案 1 :(得分:6)

我遇到了Hadoop这个问题。它在共享库中使用旧版httpclient-4.2.5.jarhttpcore-4.2.5.jar

我通过maven-shade-plugin

对部分进行着色来解决这个问题
<relocations>
    <relocation>
        <pattern>org.apache.http</pattern>
        <shadedPattern>shaded.org.apache.http</shadedPattern>
    </relocation>
</relocations>

答案 2 :(得分:5)

引起:java.lang.NoSuchFieldError:INSTANCE

java.lang.NoSuchFieldError:INSTANCE 的解决方案之一:如果我们的类路径中有两个相同类的diff版本,就会发生这种情况.... [...],所以我们首先找到该类(一个版本的类),单击该类,选择“构建路径”,然后单击“从构建路径中删除”。由333ccc333

答案 3 :(得分:4)

我有这个问题。使用HttpClientBuilder.create()。build()初始化HttpClient时看起来有问题。如果你想要更直接的解决方案,只需使用新的DefaultHttpClient()来初始化HttpClient。

HttpClient client = new DefaultHttpClient();

答案 4 :(得分:3)

此代码有效......没有任何错误..如果您使用类似的导入,请检查包。

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();

        }

    }
}

答案 5 :(得分:1)

对于那些使用Webpshere的人,请确保您的类加载策略设置为&#34; Parent Last&#34;否则它将无效,因为WAS使用的是自己的commons http版本,这可能会有冲突。

答案 6 :(得分:0)

我也有这个问题,我意识到当我们升级到java 1.8时,我只是降级到1.7并按预期工作。 不确定版本为何成为问题。

答案 7 :(得分:0)

对此和Eclipse我也感到沮丧,直到我意识到类似于Pat B的Webpshere技巧,如果依赖性顺序错误,确实会给Eclipse带来问题。

Properties -> Java Build Path -> Order and Export

在这里按核心和客户端的顺序进行操作。

答案 8 :(得分:0)

我也有这个错误,在我的类路径中
我有 httpclient-4.4.1.jarhttpcore-4.4.1.jar
但是,由于某种原因,
类加载器加载了类org.apache.http.message.BasicLineFormatter 来自 httpcore-4.0.jar 的意外位置并导致此错误
您可以使用下面的代码来检查它正在使用哪个 jar

try{
Class cls=Class.forName('org.apache.http.message.BasicLineFormatter');
  println cls.getProtectionDomain().getCodeSource().getLocation();
}catch (Error ex){
  println ex
}


希望能帮到你~