3

I have a Maven Java project that uses an HttpClient to execute HTTP requests. On my local Java Web Server everything works fine. But after I deploy it to the SAP Hana Cloud Platform I get the following error:

java.lang.NoSuchFieldError: INSTANCE
at org.apache.http.impl.io.DefaultHttpRequestWriterFactory.<init>(DefaultHttpRequestWriterFactory.java:52)
at org.apache.http.impl.io.DefaultHttpRequestWriterFactory.<init>(DefaultHttpRequestWriterFactory.java:56)
at org.apache.http.impl.io.DefaultHttpRequestWriterFactory.<clinit>(DefaultHttpRequestWriterFactory.java:46)
at org.apache.http.impl.conn.ManagedHttpClientConnectionFactory.<init>(ManagedHttpClientConnectionFactory.java:72)
at org.apache.http.impl.conn.ManagedHttpClientConnectionFactory.<init>(ManagedHttpClientConnectionFactory.java:84)
at org.apache.http.impl.conn.ManagedHttpClientConnectionFactory.<clinit>(ManagedHttpClientConnectionFactory.java:59)
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager$InternalConnectionFactory.<init>(PoolingHttpClientConnectionManager.java:493)
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.<init>(PoolingHttpClientConnectionManager.java:149)
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.<init>(PoolingHttpClientConnectionManager.java:138)
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.<init>(PoolingHttpClientConnectionManager.java:114)
at org.apache.http.impl.client.HttpClientBuilder.build(HttpClientBuilder.java:726)
at com.sap.hana.cloud.odata.service.OlingoSampleApp.getHttpclient(OlingoSampleApp.java:382)
at com.sap.hana.cloud.odata.service.OlingoSampleApp.getCsrfToken(OlingoSampleApp.java:374)
at com.sap.hana.cloud.odata.service.ODataCalls.doGet(ODataCalls.java:163)
...

My dependency looks like this:

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.3.5</version>
    <scope>compile</scope>
</dependency>

According to this question java.lang.NoSuchFieldError: org.apache.http.message.BasicLineFormatter.INSTANCE from Mashape Unirest in Java application I tried to use the following code to get the ClassLoader Ressource.

ClassLoader classLoader = this.getClass().getClassLoader();
URL resource = classLoader.getResource("org/apache/http/impl/client/HttpClientBuilder.class");
return resource;

and it returns the following

"jar:file:/some/path/WEB-INF/lib/httpclient-4.3.5.jar!/org/apache/http/impl/client/HttpClientBuilder.class"

You can see that the jar-versions are the same. Every similar question I looked at had incoherent jar-versions as the source of failure. Could there be another reason for this error?

Update

After the discussion in the comments I'll update my question:

Dependency tree:

enter image description here

So now I could see the version conflict and I removed the neo-java-web-api. The classLoader now returns version 4.3.2 again. But I still get the error from the beginning.

The new, complete dependency tree without the neo-java-web-api now looks like the following: enter image description here

Community
  • 1
  • 1
Recobe
  • 63
  • 1
  • 1
  • 9
  • Ah, but the class in conflict is BasicLineFormatter. Instead of the URL you are grabbing, try grabbing the same one from the original question: `URL resource = classLoader.getResource("org/apache/http/message/BasicLineFormatter.class");` – Pace Jun 20 '15 at 05:24
  • I've also tried this before. The response is `"jar:file:/some/path/WEB-INF/lib/httpcore-4.3.2.jar!/org/apache/http/message/BasicLineFormatter.class"`. This is the same version like my httpcore dependency. – Recobe Jun 22 '15 at 14:48
  • Can you remove the `httpcore` dep and try again. This dep is already in the [pom](https://repo1.maven.org/maven2/org/apache/httpcomponents/httpclient/4.3.5/httpclient-4.3.5.pom) of `httpclient`. –  Jun 22 '15 at 14:54
  • Ok now the version is 4.1.4. `"jar:file:some/pathWEB-INF/lib/httpcore-4.1.4.jar!/org/apache/http/message/BasicLineFormatter.class"` – Recobe Jun 22 '15 at 15:06
  • But now I Have an `java.lang.ClassNotFoundException: org.apache.http.config.Lookup` – Recobe Jun 22 '15 at 15:13
  • can you edit your question and post a `mvn dependency:tree`, seems that you still have a conflicting `httpcore` version –  Jun 22 '15 at 16:08

1 Answers1

2

I still couldn't find a solution for the actual problem. But at least I could find a workaround. I changed the scope to <scope>provided</scope> and only use functions, which are also available in version 4.1.4. For example new DefaultHttpClient(cm); instead of HttpClientBuilder.create().build();

Recobe
  • 63
  • 1
  • 1
  • 9