NoSuchFieldError INSTANCE 在 org.apache.http.impl.io.DefaultHttpRequestWriterFactory [英] NoSuchFieldError INSTANCE at org.apache.http.impl.io.DefaultHttpRequestWriterFactory

查看:36
本文介绍了NoSuchFieldError INSTANCE 在 org.apache.http.impl.io.DefaultHttpRequestWriterFactory的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

java version "1.7.0_71" 
Gradle 2.1

你好

UPDATE:

依赖

gradle dependencies | grep httpcore
|    +--- org.apache.httpcomponents:httpcore:4.3.3
|    +--- org.apache.httpcomponents:httpcore:4.3.3
|    +--- org.apache.httpcomponents:httpcore:4.3.3
|    +--- org.apache.httpcomponents:httpcore:4.3.3
|    |         |    |         |    +--- org.apache.httpcomponents:httpcore:4.1 -> 4.3.3
|    +--- org.apache.httpcomponents:httpcore:4.3.3
|    |         |    |         |    +--- org.apache.httpcomponents:httpcore:4.1 -> 4.3.3

这是否意味着我的 4.1 是到 4.3.3 的软链接?看起来很奇怪,因为当我打印出类加载器正在加载的内容时,它似乎加载了 4.3.3:/home/steve/.gradle/caches/modules-2/files-2.1/org.apache.httpcomponents/httpcore/4.3.3/f91b7a4aadc5cf486df6e4634748d7dd7a73f06d/httpcore-4.3.3.jar 看起来很奇怪.

Does this mean I have 4.1 that is a soft link to 4.3.3? Seems strange as when I print out what the class loader is loading it seems to load 4.3.3: /home/steve/.gradle/caches/modules-2/files-2.1/org.apache.httpcomponents/httpcore/4.3.3/f91b7a4aadc5cf486df6e4634748d7dd7a73f06d/httpcore-4.3.3.jar seems very strange.

当我尝试运行我的 httpClient 时,我不断收到此错误.一切编译正常.

I keep getting this error when I try and run my httpClient. Everything compiles ok.

java.lang.NoSuchFieldError: INSTANCE
        at org.apache.http.impl.io.DefaultHttpRequestWriterFactory.<init>(DefaultHttpRequestWriterFactory.java:52

我的代码很简单,去掉了不重要的代码以保持小:

My code is very simple, removed not important code to keep it small:

public class GenieClient {
    private CloseableHttpClient mClient;

    public GenieClient() {
        ClassLoader classLoader = GenieClient.class.getClassLoader();
        URL resource = classLoader.getResource("org/apache/http/message/BasicLineFormatter.class");
        log.log(Level.INFO, "resource: " + resource);

        mClient = HttpClientBuilder.create().build();
    }

    public int sendRequest() {   
        int responseCode = -1;

        try {
            HttpResponse response = mClient.execute(new HttpPost("http://www.google.com"));
            responseCode = response.getStatusLine().getStatusCode();
        }
        catch(IOException ex) {
            log.log(Level.SEVERE, "IOException: " + ex.getMessage());
        }

        return responseCode;
    }
}

我从 classLoader 得到的输出是这样的:

The output I get from the classLoader is this:

INFO: resource: jar:file:/home/steve/.gradle/caches/modules-2/files-2.1/org.apache.httpcomponents/httpcore/4.3.3/f91b7a4aadc5cf486df6e4634748d7dd7a73f06d/httpcore-4.3.3.jar!/org/apache/http/message/BasicLineFormatter.class

我使用 gradle 作为我的构建工具,并在我的 build.gradle 文件中设置了这样的依赖项:

I am using gradle as my build tool and have set the dependencies like this in my build.gradle file:

dependencies {
    compile 'org.apache.httpcomponents:httpclient:4.3.6'
}

我也尝试包含以下 httpcore,但仍然出现相同的错误:

I have also tried to include the following httpcore, but still get the same error:

compile 'org.apache.httpcomponents:httpcore:4.4'

一切正常,只有当我运行我的 httpClient 时,

Everything builds ok, its only when I run my httpClient,

非常感谢您的任何建议,

Many thanks for any suggestions,

推荐答案

org.apache.httpcomponents:httpcore:4.1 ->4.3.3 表示4.1 是请求的版本,通过冲突解决方式解析为4.3.3.执行类似 gradle -q dependencyInsight --configuration compile --dependency httpcore 之类的命令应该会让你对此有更多的了解.

org.apache.httpcomponents:httpcore:4.1 -> 4.3.3 means 4.1 is the requested version, which is resolved to 4.3.3 by conflict resolution. Executing something like gradle -q dependencyInsight --configuration compile --dependency httpcore should give you more insight on this.

您应该检查构建输出,并查看您的应用程序实际打包了哪些 jar.此外,使用一些文件管理器通过构建输出(当然包括档案)运行搜索 BasicLineFormatter.class ,这应该会给你一个明确的答案,哪些 jar 包含这个类的哪个版本.

You should check the build output, and see what jars are actually packaged with your app. Also, using some file manager run a search for BasicLineFormatter.class through the build output (including archives, of course), this should give you a definite answer what jars contain what version of this class.

如果只有一个版本 (4.3.3),这一定意味着另一个版本来自运行您的应用的容器.解决这个问题的方法取决于容器.

If there is only one version (4.3.3) this must mean that the other version comes from the container that's running your app. Ways of resolving this depend on the container.

如果确实找到多个版本,可以尝试在项目中全局排除对 httpcore 的传递依赖(用户指南).

If you do find multiple versions, you can try excluding transitive dependency to httpcore globally in the project (chapter 51.4.7 of user guide).

如果您为特定配置定义了排除项,则在解析此配置或任何继承配置时,将针对所有依赖项过滤排除的传递依赖项.

If you define an exclude for a particular configuration, the excluded transitive dependency will be filtered for all dependencies when resolving this configuration or any inheriting configuration.

这篇关于NoSuchFieldError INSTANCE 在 org.apache.http.impl.io.DefaultHttpRequestWriterFactory的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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