6

我想使用 apache httpclient 与 web 服务交互。具体来说,我正在使用 http://mirror.arcor-online.net/www.apache.org//httpcomponents/httpclient/binary/httpcomponents-client-4.3-bin.zip

因此,在 MATLAB 中,我将所有包含的 jar 加载到 javaclasspath 中,然后我可以创建客户端实例以使用:

在 MATLAB 2007b 和 2011b 中(这些是我拥有的版本):

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

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

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)

这真的让我很烦,因为 2011b 和 2012b 中的代码是相同的,所有 jar 都已加载,并且 MATLAB 附带的两个 Java 版本也是相同的(1.6.0_17-b04)。所以我真的不明白为什么类在 2012b 中变得无法加载。

我尝试使用 matlab 类加载器 - 但这让我更加困惑:

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

我还尝试将所有 jar 文件放到静态类路径中——这也没有任何效果。很高兴关于如何解决这个问题或如何找到问题的实际原因的任何想法......

4

1 回答 1

4

您的类路径中的 httpclient 和 httpcore 版本不匹配。您似乎只升级了 httpclient 但没有升级它的依赖项。您可以在 Apache HttpClient 项目页面上查看依赖项列表。http://hc.apache.org/httpcomponents-client-4.3.x/httpclient/dependencies.html

您还应该确保类路径上没有多个版本的依赖项。似乎 MATLAB 2012b 已经有一个 httpcore 版本,这就是您遇到此冲突的原因。

于 2013-12-31T20:02:45.387 回答