Java应用程序中的Mashape Unirest的java.lang.NoSuchFieldError:org.apache.http.message.BasicLineFormatter.INSTANCE [英] java.lang.NoSuchFieldError: org.apache.http.message.BasicLineFormatter.INSTANCE from Mashape Unirest in Java application

查看:307
本文介绍了Java应用程序中的Mashape Unirest的java.lang.NoSuchFieldError:org.apache.http.message.BasicLineFormatter.INSTANCE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Maven Java项目,它使用 Mashape Unirest 进行发送对其他URL的HTTP请求。我目前正在编写使用Unirest发送正常HTTP请求的集成测试(使用 TestNG )。当我通过Maven(通过Failsafe插件)运行集成测试时,请求被成功发送出去。但是,当我尝试通过Eclipse运行集成测试时,我继续收到以下错误:

I have a Maven Java project that uses Mashape Unirest for sending HTTP requests to other URLs. I am currently writing an integration test (using TestNG) that sends a normal HTTP request using Unirest. When I run the integration test through Maven (via the Failsafe plugin), the request is sent out successfully. However, when I try to run the integration test via Eclipse, I keep on getting the following error:

FAILED: getCurrentTimeTest
java.lang.NoSuchFieldError: INSTANCE
    at org.apache.http.impl.io.DefaultHttpRequestWriterFactory.<init>(DefaultHttpRequestWriterFactory.java:52)
    at org.apache.http.impl.io.DefaultHttpRequestWriterFactory.<init>(DefaultHttpRequestWriterFactory.java:56)
    at org.apache.http.impl.io.DefaultHttpRequestWriterFactory.<clinit>(DefaultHttpRequestWriterFactory.java:46)
    at org.apache.http.impl.conn.ManagedHttpClientConnectionFactory.<init>(ManagedHttpClientConnectionFactory.java:72)
    at org.apache.http.impl.conn.ManagedHttpClientConnectionFactory.<init>(ManagedHttpClientConnectionFactory.java:84)
    at org.apache.http.impl.conn.ManagedHttpClientConnectionFactory.<clinit>(ManagedHttpClientConnectionFactory.java:59)
    at org.apache.http.impl.conn.PoolingHttpClientConnectionManager$InternalConnectionFactory.<init>(PoolingHttpClientConnectionManager.java:487)
    at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.<init>(PoolingHttpClientConnectionManager.java:147)
    at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.<init>(PoolingHttpClientConnectionManager.java:136)
    at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.<init>(PoolingHttpClientConnectionManager.java:112)
    at org.apache.http.impl.client.HttpClientBuilder.build(HttpClientBuilder.java:726)
    at com.mashape.unirest.http.options.Options.refresh(Options.java:41)
    at com.mashape.unirest.http.options.Options.<clinit>(Options.java:27)
    at com.mashape.unirest.http.HttpClientHelper.prepareRequest(HttpClientHelper.java:141)
    at com.mashape.unirest.http.HttpClientHelper.requestAsync(HttpClientHelper.java:80)
    at com.mashape.unirest.request.BaseRequest.asStringAsync(BaseRequest.java:56)
    at ...

我也是能够使用基本的Java应用程序脚本重现此错误。

I am also able to reproduce this error using a basic Java application script.

我已确保我在 pom.xml 文件中使用的依赖项是最新的和最好的,如下所示:

I have made sure that the dependencies I am using in my pom.xml file are the latest and greatest, as seen below:

<dependency>
    <groupId>com.mashape.unirest</groupId>
    <artifactId>unirest-java</artifactId>
    <version>1.3.5</version>
</dependency>
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.3.2</version>
</dependency>
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpasyncclient</artifactId>
    <version>4.0</version>
</dependency>
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpmime</artifactId>
    <version>4.3.2</version>
</dependency>
<dependency>
    <groupId>org.json</groupId>
    <artifactId>json</artifactId>
    <version>20140107</version>
</dependency>
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpcore</artifactId>
    <version>4.3.2</version>
</dependency>

我还检查了 BasicLineFormatter.java的源代码,来自下载到Eclipse的源文件和 Apache的Httpcore Github repo 。在Github仓库中,注意如何为4.3.x分支和trunk分支定义 INSTANCE 字段,但不在4.2.x之类的旧分支中定义。但是,我确实在我的项目中使用了版本 4.3.2 ,所以我应该为Httpcore使用JAR文件,该文件具有最新版本的 BasicLineFormatter 。我知道,基于我项目中的Maven Dependencies JAR文件,我确实使用了这些Apache依赖项的最新版本,而不是指定为我项目的下游依赖项的旧版本。

I have also checked out the source code of BasicLineFormatter.java, both from the source file downloaded to Eclipse and from Apache's Httpcore Github repo. In the Github repo, notice how the INSTANCE field is defined for the 4.3.x branch and the trunk branch, but not in older branches like 4.2.x. However, I am indeed using version 4.3.2 in my project, so I should be using a JAR file for Httpcore that has the latest version of BasicLineFormatter. I know that, based on the Maven Dependencies JAR files that are in my project, that I am indeed using the latest versions of these Apache dependencies, not the older versions specified as downstream dependencies of my project.

我已经检查过有关此问题的其他各种SOF和博客文章,例如 Mashape Unirest Java:java.lang.NoClassDefFoundError 这篇博文也是,但他们似乎都在谈论解决 Android 的NoSuchFieldError问题。但是,我正在处理独立的Java应用程序,而不是Android应用程序。

I have checked other various SOF and blog posts about this issue, such as Mashape Unirest Java : java.lang.NoClassDefFoundError and this blog post too, but they all seem to be talking about solving the NoSuchFieldError problem for Android. However, I'm dealing with a standalone Java application, not an Android application.

我无法确定如何解决此问题。任何人都知道我需要做什么?

I am at a loss in determining how to troubleshoot this issue. Anyone have any idea what I need to do?

我没有出示我的测试用例,将这个问题的再现说明简化为一个简单的单行Java应用程序,因为在Eclipse中运行的任何Java应用程序或测试用例都存在问题,而不仅仅是一个特定的测试:

Instead of showing my test case, I will reduce the illustration of a reproduction of this problem to just a simple one-liner Java application, because the problem exists with any Java application or test case run through Eclipse, not just one particular test:

System.out.println(Unirest.get("http://www.google.com").asStringAsync().get().getBody());

通常情况下,这应打印Google主页的HTML,但我会获得NoSuchFieldError堆栈跟踪。

Normally, this should print the HTML of the Google home page, but I instead get the NoSuchFieldError stack trace.

问题在于 AWS SDK (它在我的类路径上,因为我正在为Elastic Beanstalk开发)有一个冲突的JAR文件。使用Oleg的解决方案(感谢BTW),我在单元测试中打印了以下输出:

The problem was that the AWS SDK (it's on my classpath because I'm developing for Elastic Beanstalk) had a conflicting JAR file. Using Oleg's solution (thanks BTW), I printed the following output in a unit test:

jar:file:/some/path/aws-java-sdk/1.7.1/third-party/httpcomponents-client-4.2.3/httpcore-4.2.jar!/org/apache/http/message/BasicLineFormatter.class

我必须重新安排我的类路径,以便AWS SDK不再发生冲突。

I'll have to rearrange my classpath so that AWS SDK is no longer conflicting.

推荐答案

对这个问题唯一合理的解释是在类路径上有一个旧版本的HttpCore(除非你还想考虑一下火星绿色人员远程搞乱你的计算机的可能性)飞碟)。

The only plausible explanation to this problem is there is an older version of HttpCore on the classpath (unless you also want to consider a possibility of green men from Mars messing with your computer remotely from a flying saucer).

您可以将此片段添加到您的代码中,以找出该类从中获取的jar。这可能有助于首先找出jar在你的类路径上的原因。

You can add this snippet to your code to find out what jar the class gets picked up from. This might help find out why that jar is on your classpath in the first place.

ClassLoader classLoader = MyClass.class.getClassLoader();
URL resource = classLoader.getResource("org/apache/http/message/BasicLineFormatter.class");
System.out.println(resource);

这基本上告诉我,在我的情况下,jar驻留在本地maven存储库中,可能已经存在由Maven添加到类路径中

This basically tells me that in my case the jar resides in the local maven repository and likely to have been added to the classpath by Maven

jar:file:/home/oleg/.m2/repository/org/apache/httpcomponents/httpcore/4.3.1/httpcore-4.3.1.jar!/org/apache/http/message/BasicLineFormatter.class

这篇关于Java应用程序中的Mashape Unirest的java.lang.NoSuchFieldError:org.apache.http.message.BasicLineFormatter.INSTANCE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆