简体   繁体   English

org.apache.http.impl.io.DefaultHttpRequestWriterFactory中的NoSuchFieldError INSTANCE

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

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? 这是否意味着我有4.1这是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. 看起来很奇怪,因为当我打印出类加载器正在加载它时,似乎加载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似乎很奇怪。

I keep getting this error when I try and run my httpClient. 当我尝试运行我的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: 我从classLoader获得的输出是这样的:

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: 我使用gradle作为我的构建工具,并在我的build.gradle文件中设置了这样的依赖build.gradle

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

I have also tried to include the following httpcore, but still get the same error: 我也尝试包含以下httpcore,但仍然得到相同的错误:

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

Everything builds ok, its only when I run my httpClient, 一切都建立好,只有当我运行我的httpClient时,

Many thanks for any suggestions, 非常感谢任何建议,

Start your jvm with -verbose:class, it will show which class is being loaded from where. 使用-verbose:class启动你的jvm,它将显示从哪里加载哪个类。

I had such problems in the past in two scenarios: 我过去在两个场景中遇到过这样的问题:

  1. one of the jars you're loading has a manifest with Classpath clause which references incorrect version of httpcore. 您正在加载的其中一个jar有一个带有Classpath子句的清单,该子句引用了不正确的httpcore版本。

  2. you're running within some container (tomcat or sth) with own classloaders and some classes are loaded eg by the container from a different jar. 你在一个带有自己的类加载器的容器(tomcat或sth)中运行,并且某些类例如由来自不同jar的容器加载。

I also don't think your code showing resource for the class works as it explicitly uses GenieClient's classloader. 我也不认为您的代码显示该类的资源,因为它显式使用GenieClient的类加载器。 Probably using BasicLineFormatter.class.getClassloader() would give different results. 可能使用BasicLineFormatter.class.getClassloader()会产生不同的结果。 But better try with -verbose:class . 但最好尝试使用-verbose:class

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. org.apache.httpcomponents:httpcore:4.1 -> 4.3.3表示4.1是请求的版本,通过冲突解决解析为4.3.3 Executing something like gradle -q dependencyInsight --configuration compile --dependency httpcore should give you more insight on this. 执行像gradle -q dependencyInsight --configuration compile --dependency httpcore可以让你更深入地了解这一点。

You should check the build output, and see what jars are actually packaged with your app. 您应该检查构建输出,并查看实际与您的应用程序一起打包的jar。 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. 此外,使用一些文件管理器通过构建输出(当然包括档案)运行BasicLineFormatter.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. 如果只有一个版本(4.3.3),则必须表示另一个版本来自运行应用程序的容器。 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 ). 如果找到多个版本,可以尝试在项目中全局排除传递依赖关系到httpcore用户指南的第51.4.7节)。

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. 如果为特定配置定义排除,则在解析此配置或任何继承配置时,将针对所有依赖项筛选排除的传递依赖关系。

似乎BasicLineFormatter INSTANCE in 4.3-beta1 链接链接中获得了静态INSTANCE in 4.3-beta1

Clearly there is some older version of BasicLineFormatter on your classpath, one that has the DEFAULT field instead of INSTANCE . 显然,类路径上有一些旧版本的BasicLineFormatter ,它具有DEFAULT字段而不是INSTANCE See eg . 例如

But your classloader is reporting that you're using httpcore-4.3.3, and your Gradle dependency tree backs that up. 但是你的类加载器报告你正在使用httpcore-4.3.3,你的Gradle依赖树支持它。

So it seems that you have some other JAR that has an old version of BasicLineFormatter in it. 所以看起来你有一些其他的JAR里面有旧版本的BasicLineFormatter You might need to go through your dependency JARs and just look to see whether it is in there. 您可能需要查看依赖项JAR,然后查看它是否在那里。 Try a jar tf *.jar | grep 'BasicLineFormatter' 尝试一下jar tf *.jar | grep 'BasicLineFormatter' jar tf *.jar | grep 'BasicLineFormatter' to see which JARs might be involved. jar tf *.jar | grep 'BasicLineFormatter'可以查看可能涉及的JAR。

Also, this may be relevant. 此外,这可能是相关的。 I looked at your tags and it seems you do some Android development. 我看了你的标签,似乎你做了一些Android开发。 So the following Stack Overflow question may be of interest: 因此,以下Stack Overflow 问题可能会引起关注:

Even if that's not the exact issue, it shows how you might have an old BasicLineFormatter hanging around even if you have the right httpcore dependency. 即使这不是确切的问题,它也会显示即使你有正确的httpcore依赖关系,你也可能会有一个旧的BasicLineFormatter

  1. find httpcore which is of an older version than 4.3 找到比4.3更旧版本的httpcore
  2. remove the older version of httpcore jar from Referenced Libraries. 从Referenced Libraries中删除旧版本的httpcore jar。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 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 HTTPClient“主要” java.lang.NoSuchFieldError:实例位于org.apache.http.conn.ssl.SSLConnectionSocketFactory。 <clinit> - HTTPClient “main” java.lang.NoSuchFieldError: INSTANCE at org.apache.http.conn.ssl.SSLConnectionSocketFactory.<clinit> java.lang.NoClassDefFoundError:org / apache / http / impl / conn / PoolingClientConnectionManager - java.lang.NoClassDefFoundError: org/apache/http/impl/conn/PoolingClientConnectionManager org.apache.http.impl.execchain.RequestAbortedException:请求中止 - org.apache.http.impl.execchain.RequestAbortedException: Request aborted org.apache.http.impl.client.CloseableHttpClient代理验证 - org.apache.http.impl.client.CloseableHttpClient Proxy Authentication org.apache.http.client-4.3.6:java.lang.NoClassDefFoundError:org.apache.http.impl.conn.PoolingHttpClientConnectionManager - org.apache.http.client-4.3.6: java.lang.NoClassDefFoundError: org.apache.http.impl.conn.PoolingHttpClientConnectionManager 从Apache HttpCore 4.4.3将ssl与org.apache.http.impl.bootstrap.HttpServer一起使用的示例 - Example of using ssl with org.apache.http.impl.bootstrap.HttpServer from Apache HttpCore 4.4.3 如何诊断泄露的http连接(org.apache.http.impl.conn.tsccm.ConnPoolByRoute) - How to diagnose leaked http connections (org.apache.http.impl.conn.tsccm.ConnPoolByRoute) org.postgresql.jdbc.PgResultSet实例和io.vertx.core.impl.VertxThread的内存泄漏 - Memory leak of org.postgresql.jdbc.PgResultSet instance and io.vertx.core.impl.VertxThread MACOSX-java.lang.NoSuchMethodError:org.apache.http.impl.conn.CPool.setValidateAfterInactivity(I)V - MACOSX - java.lang.NoSuchMethodError: org.apache.http.impl.conn.CPool.setValidateAfterInactivity(I)V
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM