线程“main”java.lang.nosuchfielderror:示例中出现异常

mgdq6dx1  于 2021-07-09  发布在  Java
关注(0)|答案(8)|浏览(736)

我将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
有什么想法吗?

xpcnnkqh

xpcnnkqh1#

对于那些使用webpshere的用户,请确保您的类加载策略设置为“parent last”,否则它将无法工作,因为was使用的是它自己的commons http版本,这可能会发生冲突。

jgzswidk

jgzswidk2#

我也有这个问题,我意识到这是当我们升级到Java1.8,我只是降级到1.7和预期的工作。不知道为什么这个版本会成为一个问题。

ybzsozfc

ybzsozfc3#

我有这个问题。使用httpclientbuilder.create().build()初始化httpclient时似乎有问题。如果您想要更直接的解决方案,只需使用new defaulthttpclient()初始化httpclient。

HttpClient client = new DefaultHttpClient();
56lgkhnf

56lgkhnf4#

查看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 类,可能您有一个与4.3.2版本冲突的旧版本的httpcore。

vc6uscn9

vc6uscn95#

我也对这个和eclipse感到沮丧,直到我意识到,与patb的webpshere技巧类似,如果依赖项顺序错误,它确实会给eclipse带来问题。 Properties -> Java Build Path -> Order and Export 按照核心和客户的顺序在这里玩一玩。

nlejzf6q

nlejzf6q6#

这段代码工作…没有任何错误。。如果使用类似的导入,请检查包。

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

        }

    }
}
yvt65v4c

yvt65v4c7#

我在hadoop上遇到了这个问题。它使用了旧版本的 httpclient-4.2.5.jar 以及 httpcore-4.2.5.jar 在他们共享的库中。
我通过maven shade插件对部分进行着色来解决这个问题

<relocations>
    <relocation>
        <pattern>org.apache.http</pattern>
        <shadedPattern>shaded.org.apache.http</shadedPattern>
    </relocation>
</relocations>
lvjbypge

lvjbypge8#

原因:java.lang.nosuchfielderror:示例
java.lang.nosuchfielderror的解决方案之一:instance:如果类路径中有两个相同类的diff版本,就会发生这种情况….[…],因此,我们首先找到该类(类的一个版本),单击该类,选择“构建路径”,然后单击“从构建路径中移除”。通过333ccc333

相关问题