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

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

问题描述

我有一个使用 Mashape Unirest 发送 HTTP 的 Maven Java 项目对其他 URL 的请求.我目前正在编写一个集成测试(使用 TestNG),它使用 Unirest 发送一个普通的 HTTP 请求.当我通过 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 存储库.在 Github 存储库中,注意 INSTANCE 字段是如何为 4.3.x 分支和主干分支定义的,但不是在像 4.2.x 这样的旧分支中定义的.但是,我确实在我的项目中使用了 4.3.2 版本,所以我应该为 Httpcore 使用一个 JAR 文件,它具有最新版本的 BasicLineFormatter.我知道,基于我项目中的 Maven 依赖项 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 UnirestJava : 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 应用程序,因为任何 Java 应用程序或通过 Eclipse 运行的测试用例都存在问题,而不仅仅是一个特定的测试:

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 的解决方案(顺便说一句),我在单元测试中打印了以下输出:

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 开发工具包不再发生冲突.

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.lang.NoSuchFieldError: org.apache.http.message.BasicLineFormatter.INSTANCE 来自 Java 应用程序中的 Mashape Unirest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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