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

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

问题描述

  java版本1.7.0_71
Gradle 2.1

你好,

 更新:

依赖关系

  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 看起来很奇怪。



我在尝试时遇到此错误并运行我的httpClient。一切都编译好。

  java.lang.NoSuchFieldError:INSTANCE $ b $在org.apache.http.impl.io。 DefaultHttpRequestWriterFactory。< init>(DefaultHttpRequestWriterFactory.java:52 

我的代码很简单,删除不重要代码保持小:

  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 ));
responseCod e = response.getStatusLine()。getStatusCode();
}
catch(IOException ex){
log.log(Level.SEVERE,IOException:+ ex.getMessage());
}

return responseCode;




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

$ / p>

  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 文件中设置了这样的依赖项:

 依赖项{
compile'org.apache.httpcomponents:httpclient:4.3.6'
}

我也尝试加入下面的httpcore,但仍然得到相同的错误:

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

一切都可以,只有当我运行我的httpClient时,



非常感谢任何建议,

解决方案

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



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

如果只有一个版本(4.3.3),则这意味着其他版本来自正在运行应用程序的容器。如果你找到了多个版本,你可以尝试排除传递依赖到 httpcore 在项目中全局化(用户指南的第51.4.7章)。


如果您为特定配置定义排除,则在解析此配置或任何继承配置时,将排除所有依赖关系的排除传递依赖关系。



java version "1.7.0_71" 
Gradle 2.1

Hello,

UPDATE:

The dependencies

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

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.

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;
    }
}

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

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'
}

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

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

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

Many thanks for any suggestions,

解决方案

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.

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.

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.

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.

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

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