ant2009 ant2009 - 20 days ago 7
Java Question

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? 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,

Answer

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.