6

I want to use the apache httpclient for interaction with webservices. Specifically I'm using http://mirror.arcor-online.net/www.apache.org//httpcomponents/httpclient/binary/httpcomponents-client-4.3-bin.zip

So in MATLAB I'm loading all the included jars into the javaclasspath and then I can create to client-instance to work with:

In MATLAB 2007b and 2011b (those are the versions I have):

client = org.apache.http.impl.client.HttpClients.createDefault()
client =

org.apache.http.impl.client.InternalHttpClient@1e89831

In 2012b:

>> client = org.apache.http.impl.client.HttpClients.createDefault()

Java exception occurred:
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:487)

    at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.<init>(PoolingHttpClientConnectionManager.java:147)

    at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.<init>(PoolingHttpClientConnectionManager.java:136)

    at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.<init>(PoolingHttpClientConnectionManager.java:112)

    at org.apache.http.impl.client.HttpClientBuilder.build(HttpClientBuilder.java:710)

    at org.apache.http.impl.client.HttpClients.createDefault(HttpClients.java:58)


>> client = org.apache.http.impl.client.HttpClients.createDefault()
Java exception occurred:
java.lang.NoClassDefFoundError: Could not initialize class org.apache.http.impl.conn.ManagedHttpClientConnectionFactory

    at
    org.apache.http.impl.conn.PoolingHttpClientConnectionManager$InternalConnectionFactory.<init>(PoolingHttpClientConnectionManager.java:487)

    at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.<init>(PoolingHttpClientConnectionManager.java:147)

    at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.<init>(PoolingHttpClientConnectionManager.java:136)

    at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.<init>(PoolingHttpClientConnectionManager.java:112)

    at org.apache.http.impl.client.HttpClientBuilder.build(HttpClientBuilder.java:710)

    at org.apache.http.impl.client.HttpClients.createDefault(HttpClients.java:58)

This is really bugging me, since code in 2011b and 2012b is identical, all jar's are loaded, and both Java-Versions coming with MATLAB are identical as well (1.6.0_17-b04). So I really don't get why classes have become unloadable in 2012b.

I tried playing around with the matlab classloader - but this confuses me even more:

>> jloader =  com.mathworks.jmi.ClassLoaderManager.getClassLoaderManager();
>> jloader.loadClass('org.apache.http.impl.conn.ManagedHttpClientConnectionFactory')
Java exception occurred:
java.lang.NoClassDefFoundError: Could not initialize class org.apache.http.impl.conn.ManagedHttpClientConnectionFactory

    at java.lang.Class.forName0(Native Method)

    at java.lang.Class.forName(Unknown Source)

    at com.mathworks.jmi.ClassLoaderManager.loadClass(ClassLoaderManager.java:440)


>> jloader.getCurrentClassLoader.loadClass('org.apache.http.impl.conn.ManagedHttpClientConnectionFactory')

ans =

class org.apache.http.impl.conn.ManagedHttpClientConnectionFactory

I also tried putting all jar-files onto the static classpath - which didn't have any effect either. Glad about any ideas on how solve this or how find the actual reason for the issue...

Amro
  • 123,847
  • 25
  • 243
  • 454
sebastian
  • 9,526
  • 26
  • 54
  • I havent checked but perhaps MATLAB already ships with a (possibly older) version of this library for its own use? – Amro Sep 18 '13 at 13:55
  • This might help in diagnosing the problem: http://stackoverflow.com/a/4380622/97160 . If it is indeed an issue of conflicting versions, there are workarounds to override MATLAB jars: http://stackoverflow.com/q/16366059/97160 – Amro Sep 18 '13 at 14:13
  • Hm, but then `methodsview org.apache.http.impl.conn.ManagedHttpClientConnectionFactory` should be showing something even before I add the Apache jar-files to the javaclasspath, right? That's not the case, it errors out with `No class found...` – sebastian Sep 18 '13 at 14:34
  • oh well it was just an idea :/ can you try to use the static java class path instead of dynamically adding to MATLAB? That is also a common source of problems.. http://www.mathworks.com/help/matlab/matlab_external/bringing-java-classes-and-methods-into-matlab-workspace.html – Amro Sep 18 '13 at 14:41
  • I tried that - to no visible effect, as mentioned in the question. – sebastian Sep 18 '13 at 14:53
  • As an aside: I actually came across the issue because I provided matlab code to a colleague using 2012 whereas I'm usually working in 2011b - and he first came across this. So it's also no pathological issue with my individual setup... – sebastian Sep 18 '13 at 14:56
  • ok I'll download the library and try for myself (I have R2013a).. – Amro Sep 18 '13 at 14:58
  • so I [tried](http://pastebin.com/MkdMDKue) this but no success. The error message suggests that the library is using the class loader on its own which may not be compatible with the way MATLAB works... fyi as I suspected, MATLAB ships with the older "Commons HttpClient 3.x" (superseded by Apache HttpClient 4.x), and it seems to work just fine: http://pastebin.com/QkF4iPbJ . Honestly I dont know how to fix it for your case, sorry.. – Amro Sep 18 '13 at 15:47
  • There are others who reported the same problem: http://stackoverflow.com/q/14226128/97160, http://stackoverflow.com/questions/11064979/how-to-send-data-from-matlab-to-rails#comment19734127_11066252 – Amro Sep 18 '13 at 15:58
  • Thanks for the effort though. Turns out MATLAB's java is also affected by this issue: http://www.oracle.com/technetwork/java/javase/documentation/tlsreadme2-176330.html which prevents me from using it in my current context - i might try to look into `curl` instead :> – sebastian Sep 18 '13 at 17:29
  • alright, good luck. Although that TLS/SSL issue shouldn't stop you from using a newer JRE6 for MATLAB on your machine (by [setting](http://www.mathworks.com/support/solutions/en/data/1-1812J/) the `MATLAB_JAVA` environment variable).. btw you'd be happy to know that MATLAB finally upgraded to Java 7 in R2013b – Amro Sep 18 '13 at 17:40

1 Answers1

4

You have mismatched versions of httpclient and httpcore on your classpath. It appears that you have upgraded only httpclient but none of its dependencies. You can see a dependency list on the Apache HttpClient project page. http://hc.apache.org/httpcomponents-client-4.3.x/httpclient/dependencies.html

You should also make sure you don't have multiple versions of dependencies on the classpath. It seems like MATLAB 2012b already has a version of httpcore which is why you're encountering this conflict.

JRS
  • 878
  • 9
  • 9
  • That easily happens when you install something with maven and you don't notice that maven installs jars that you already have but different versions. I did that today :-) – Christine Mar 15 '14 at 21:40