Java Http Client - No Such Field Error

2k Views Asked by At

I am trying to use the Digital Ocean java library and when trying to create a connection with:

CloseableHttpClient httpClient = HttpClients.createDefault();

client = new DigitalOceanClient("v2", token, httpClient);

The problem lies in the CloseableHttpClient httpClient object. The library uses httpcore version 4.4.1 and httpclient version 4.5. It gives me a java.lang.NoSuchFieldError.

Here's my log:

Exception in thread "main" java.lang.NoSuchFieldError: INSTANCE
at org.apache.http.conn.ssl.SSLConnectionSocketFactory.<clinit>(SSLConnectionSocketFactory.java:144)
at org.apache.http.impl.client.HttpClientBuilder.build(HttpClientBuilder.java:955)
at org.apache.http.impl.client.HttpClients.createDefault(HttpClients.java:58)
at main.java.servers.DigitalOceanServer.setup(DigitalOceanServer.java:28)

I found little information and documentation on the NoSuchFieldError, so I'm assuming it has to do with the version of httpcore and/or httpclient that causes this error.

1

There are 1 best solutions below

0
Kohei TAMURA On

The field variable INSTANCE has been introduced in AllowAllHostnameVerifier.java since Apache httpclient version 4.4.

If both new version of SSLConnectionSocketFactory and old version of AllowAllHostnameVerifier is used, then java.lang.NoSuchFieldError: INSTANCE is thrown.

One of the possible cause is two version of Apache httpclient jar are in your library directory.