wildflyapache-httpcomponents

java.lang.NoSuchFieldError: INSTANCE in Apache HttpComonents and Wildfly


I get the following error when running an app using HttpComponents 4.3.6 in Wildfly. How can I fix this?

Caused by: java.lang.NoSuchFieldError: INSTANCE
at org.apache.http.impl.io.DefaultHttpRequestWriterFactory.<init>(DefaultHttpRequestWriterFactory.java:52) [httpcore-4.3.3.jar:4.3.3]
at org.apache.http.impl.io.DefaultHttpRequestWriterFactory.<init>(DefaultHttpRequestWriterFactory.java:56) [httpcore-4.3.3.jar:4.3.3]
at org.apache.http.impl.io.DefaultHttpRequestWriterFactory.<clinit>(DefaultHttpRequestWriterFactory.java:46) [httpcore-4.3.3.jar:4.3.3]
at org.apache.http.impl.conn.ManagedHttpClientConnectionFactory.<init>(ManagedHttpClientConnectionFactory.java:72) [httpclient-4.3.6.jar:4.3.6]
at org.apache.http.impl.conn.ManagedHttpClientConnectionFactory.<init>(ManagedHttpClientConnectionFactory.java:84) [httpclient-4.3.6.jar:4.3.6]
at org.apache.http.impl.conn.ManagedHttpClientConnectionFactory.<clinit>(ManagedHttpClientConnectionFactory.java:59) [httpclient-4.3.6.jar:4.3.6]
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager$InternalConnectionFactory.<init>(PoolingHttpClientConnectionManager.java:494) [httpclient-4.3.6.jar:4.3.6]
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.<init>(PoolingHttpClientConnectionManager.java:149) [httpclient-4.3.6.jar:4.3.6]
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.<init>(PoolingHttpClientConnectionManager.java:138) [httpclient-4.3.6.jar:4.3.6]
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.<init>(PoolingHttpClientConnectionManager.java:114) [httpclient-4.3.6.jar:4.3.6]
at org.apache.http.impl.client.HttpClientBuilder.build(HttpClientBuilder.java:726) [httpclient-4.3.6.jar:4.3.6]
at org.apache.http.impl.client.HttpClients.createDefault(HttpClients.java:58) [httpclient-4.3.6.jar:4.3.6]

Solution

  • Every post on the subject says that this is because of a conflict between different versions of HttpComponents. In my case it was a conflict between the version I imported into my app, and the version supplied with Wildfly.

    To work around it, you'll need a file called WEB-INF/jboss-deployment-structure.xml with the following:

    <jboss-deployment-structure>
    <deployment>
        <exclusions>
            <module name="org.apache.httpcomponents" />
        </exclusions>
    </deployment>
    

    That will prevent wildfly from loading the older version and interfering with your new version.

    See https://docs.jboss.org/author/display/WFLY8/Class+Loading+in+WildFly for more details.