HTTPClient 示例 - 线程“main”中的异常 java.lang.NoSuchFieldError: INSTANCE
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22330848/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
HTTPClient Example - Exception in thread "main" java.lang.NoSuchFieldError: INSTANCE
提问by jagamot
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
- Httpcore-4.3.2.jar
- Httpclient-4.3.3.jar
- Httpcore-4.3.2.jar
Any ideas?
有任何想法吗?
采纳答案by Jay
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();
}
}
}
回答by vzamanillo
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? DefaultHttpRequestWriterFactory
try to resolve
您确定您使用的是 HttpCore 4.3.2 吗?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。
回答by vijay m p
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…. […], 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
java.lang.NoSuchFieldError: INSTANCE的解决方案之一:如果我们的类路径中有两个相同类的不同版本,就会发生这种情况...... […],所以我们首先找到那个类(一个版本的类),点击那个类,选择“构建路径”,然后我们点击“从构建路径中删除”。由 333ccc333
回答by H6.
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.
我在使用 Hadoop 时遇到了这个问题。它使用了旧版本的httpclient-4.2.5.jar
和httpcore-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>
回答by Hardik Rakholiya
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.
我有这个问题。使用 HttpClientBuilder.create().build() 初始化 HttpClient 时似乎存在问题。如果您想要更直接的解决方案,只需使用 new DefaultHttpClient() 来初始化 HttpClient。
HttpClient client = new DefaultHttpClient();
回答by Pat B
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.
对于那些使用 Webpshere 的人,请确保您的类加载策略设置为“Parent Last”,否则它将无法工作,因为 WAS 使用的是它自己的 commons http 版本,这可能会发生冲突。
回答by B.J. A.A.
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.
我也有这个问题,我意识到这是当我们升级到 java 1.8 时,我只是降级到 1.7 并按预期工作。不知道为什么版本成为问题。
回答by 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.
我也对此和 Eclipse 感到沮丧,直到我意识到与 Pat B 的 Webpshere 提示类似,如果您的依赖项顺序错误,它确实会导致 Eclipse 出现问题。
Properties -> Java Build Path -> Order and Export
Properties -> Java Build Path -> Order and Export
Play a bit around here with the order of core and client.
按照核心和客户端的顺序在这里玩一下。