简体   繁体   English

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

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

I am using HttpClient components from Apache for the following simple program and I see the below exception:我将 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();
        }
    }
}

I am using我在用

  • Httpclient-4.3.3.jar Httpclient-4.3.3.jar
  • Httpcore-4.3.2.jar Httpcore-4.3.2.jar

Any ideas?有任何想法吗?

I had this problem with Hadoop.我在使用 Hadoop 时遇到了这个问题。 It used an old version of httpclient-4.2.5.jar and httpcore-4.2.5.jar in their shared lib.它在共享库中使用了旧版本的httpclient-4.2.5.jarhttpcore-4.2.5.jar

I solved this by shading parts via the maven-shade-plugin我通过maven-shade-plugin对部分进行着色解决了这个问题

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

Looking at the source code of DefaultHttpRequestWriterFactory查看 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?你确定你使用的是 HttpCore 4.3.2? DefaultHttpRequestWriterFactory try to resolve DefaultHttpRequestWriterFactory尝试解决

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.检查您的类路径中可能包含另一个BasicLineFormatter类的库,也许您有一个与 4.3.2 版本冲突的旧版本的 HttpCore。

Caused by: java.lang.NoSuchFieldError: INSTANCE引起: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…. java.lang.NoSuchFieldError: INSTANCE的解决方案之一如果我们的类路径中有两个相同类的不同版本,就会发生这种情况...... […], 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由 333ccc333

I had this problem.我有这个问题。 It looks like there is a problem while initializing HttpClient with HttpClientBuilder.create().build().使用 HttpClientBuilder.create().build() 初始化 HttpClient 时似乎存在问题。 If you want more immediate solution just use new DefaultHttpClient() to initialize HttpClient.如果您想要更直接的解决方案,只需使用 new DefaultHttpClient() 来初始化 HttpClient。

HttpClient client = new DefaultHttpClient();

This code works...without any error.. check the packages if you are using similar import .此代码有效......没有任何错误......如果您使用类似的 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();

        }

    }
}

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

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.我也有这个问题,我意识到这是当我们升级到 java 1.8 时,我只是降级到 1.7 并按预期工作。 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.我也对此和 Eclipse 感到沮丧,直到我意识到与 Pat B 的 Webpshere 提示类似,如果您的依赖项顺序错误,它确实会导致 Eclipse 出现问题。

Properties -> Java Build Path -> Order and Export

Play a bit around here with the order of core and client.按照核心和客户端的顺序在这里玩一下。

I have this error too, in my class path我也有这个错误,在我的类路径中
I have httpclient-4.4.1.jar , and httpcore-4.4.1.jar我有httpclient-4.4.1.jarhttpcore-4.4.1.jar
However, for some reason,然而,出于某种原因,
the classloader loaded the class org.apache.http.message.BasicLineFormatter from httpcore-4.0.jar in a unexpected location and caused this error类加载器在意外位置从httpcore-4.0.jar加载类org.apache.http.message.BasicLineFormatter并导致此错误
you can use below code to check which jar it is using您可以使用下面的代码来检查它正在使用哪个 jar

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


Hope this help~希望这有帮助~

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 线程“主”中的异常java.lang.NoSuchFieldError:AWS SNS中的INSTANCE - Exception in thread “main” java.lang.NoSuchFieldError: INSTANCE in AWS SNS 线程“主”java.lang.NoSuchFieldError 中的异常:实例 - Exception in thread "main" java.lang.NoSuchFieldError: INSTANCE 如何解决线程“main”中的异常 java.lang.NoSuchFieldError: INSTANCE? - How to solve Exception in thread “main” java.lang.NoSuchFieldError: INSTANCE? “线程“主”中的异常 java.lang.NoSuchFieldError:” - "Exception in thread "main" java.lang.NoSuchFieldError:" 线程“ main”中的异常java.lang.NoSuchFieldError:JAVA_VENDOR - Exception in thread “main” java.lang.NoSuchFieldError: JAVA_VENDOR 线程“ main”中的异常java.lang.NoSuchFieldError:文件系统 - Exception in thread “main” java.lang.NoSuchFieldError: filesystem 线程“main”中的异常 java.lang.NoSuchFieldError: LINUX - Exception in thread "main" java.lang.NoSuchFieldError: LINUX 线程“ main”中的异常java.lang.NoSuchFieldError:VERSION_5 - Exception in thread “main” java.lang.NoSuchFieldError: VERSION_5 线程“ main”中的异常java.lang.NoSuchFieldError:ruleMemo - Exception in thread “main” java.lang.NoSuchFieldError: ruleMemo 线程“ main”中的异常java.lang.NoSuchFieldError:如果可能 - Exception in thread “main” java.lang.NoSuchFieldError: ifpossible
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM