Java Error - Exception in thread "main" java.lang.NoSuchFieldError: DEF_CONTENT_CHARSET


Many of you will get this exception when using HttpClient in your java application


Exception in thread "main" java.lang.NoSuchFieldError: DEF_CONTENT_CHARSET
 at org.apache.http.impl.client.DefaultHttpClient.setDefaultHttpParams(DefaultHttpClient.java:175)
 at org.apache.http.impl.client.DefaultHttpClient.createHttpParams(DefaultHttpClient.java:158)
 at org.apache.http.impl.client.AbstractHttpClient.getParams(AbstractHttpClient.java:448)
 at org.apache.http.impl.client.AbstractHttpClient.createClientConnectionManager(AbstractHttpClient.java:309)
 at org.apache.http.impl.client.AbstractHttpClient.getConnectionManager(AbstractHttpClient.java:466)
 at org.apache.http.impl.client.AbstractHttpClient.createHttpContext(AbstractHttpClient.java:286)
 at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:851)
 at org.apache.http.impl.client.DecompressingHttpClient.execute(DecompressingHttpClient.java:137)
 at org.apache.http.impl.client.DecompressingHttpClient.execute(DecompressingHttpClient.java:108)


Problem:

  • You got multiple versions of HttpClient
  • Incompatible httpclient.jar and httpcore.jar
For example: Using httpclient-4.2.3.jar and http-core-4.1.jar together will throw this exception. These two jar files are not compatible.
Using httpcore-4.4.jar and httpclient-4.4.jar together will work just fine.

If you have both httpclient-4.4.jar and httpclient-4.2.3.jar, you are most likely to get exceptions. Please remove the older one from your project build path.

Resolution:
  • Remove duplicate versions of HttpClient
  • Use compatible versions of httpclient.jar and httpcore.jar (Use the latest versions if possible).





Reactions:

0 comments :

Post a Comment