问题
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,
回答1:
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.
回答2:
Seems like BasicLineFormatter
got its static INSTANCE in 4.3-beta1
link and link
回答3:
Start your jvm with -verbose:class, it will show which class is being loaded from where.
I had such problems in the past in two scenarios:
one of the jars you're loading has a manifest with Classpath clause which references incorrect version of httpcore.
you're running within some container (tomcat or sth) with own classloaders and some classes are loaded e.g. by the container from a different jar.
I also don't think your code showing resource for the class works as it explicitly uses GenieClient's classloader. Probably using BasicLineFormatter.class.getClassloader()
would give different results.
But better try with -verbose:class
.
回答4:
Clearly there is some older version of BasicLineFormatter
on your classpath, one that has the DEFAULT
field instead of INSTANCE
. See e.g.
But your classloader is reporting that you're using httpcore-4.3.3, and your Gradle dependency tree backs that up.
So it seems that you have some other JAR that has an old version of BasicLineFormatter
in it. You might need to go through your dependency JARs and just look to see whether it is in there. Try a jar tf *.jar | grep 'BasicLineFormatter'
to see which JARs might be involved.
Also, this may be relevant. I looked at your tags and it seems you do some Android development. So the following Stack Overflow question may be of interest:
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.
回答5:
- find httpcore which is of an older version than 4.3
- remove the older version of httpcore jar from Referenced Libraries.
来源:https://stackoverflow.com/questions/27921077/nosuchfielderror-instance-at-org-apache-http-impl-io-defaulthttprequestwriterfac