Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apache HttpClient in MATLAB 2012+

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...

like image 451
sebastian Avatar asked Sep 18 '13 10:09

sebastian


People also ask

What is HTTP client in Apache?

Apache HttpClient - Overview The Hypertext Transfer Protocol (HTTP) is an application-level protocol for distributed, collaborative, hypermedia information systems. This is the foundation for data communication for the World Wide Web (i.e., Internet) since 1990.

Who should use httpclient?

Designed for extension while providing robust support for the base HTTP protocol, HttpClient may be of interest to anyone building HTTP-aware client applications such as web browsers, web service clients, or systems that leverage or extend the HTTP protocol for distributed communication.

How to install httpclient in Eclipse?

Right click on the project select the option Build Path → Configure Build Path as shown below. In the Java Build Path frame in the Libraries tab, click on Add External JARs. And select all the jar files in the lib folder and, click on Apply and Close. You are all set to work with HttpClient library in eclipse.

What is httpclient in Laravel?

HttpClient is a more advanced class, and was added later, than other classes like WebClient. This class is built for asynchronous use. For downloading web pages, it is better to enable and support GZIP compression. Another header can be added to HttpClient.


1 Answers

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.

like image 145
JRS Avatar answered Oct 23 '22 05:10

JRS