Вопрос

I'm trying to use the Apache DefaultHttpClient to execute a JSON POST request and it's giving me a NoClassDefFound Error when i try to instantiate it.

HttpClient client = new DefaultHttpClient();

I've included the following jars in my buildpath,

httpclient-4.2.5.jar httpcore-4.2-alpha2.jar json-simple-1.1.1.jar fluent-hc-4.2.5.jar httpclient-cache-4.2.5.jar httpmime-4.2.5.jar org.apache.httpcomponents.httpclient_4.2.5.jar

I have no problem when I try to import DefaultHttpClient as follows

   import org.apache.http.impl.client.DefaultHttpClient;

but only when I try and make an instance of it as mentioned above.

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
    at org.apache.http.impl.client.AbstractHttpClient.<init>(AbstractHttpClient.java:187)
    at org.apache.http.impl.client.DefaultHttpClient.<init>(DefaultHttpClient.java:146)
    at AtlasService.main(AtlasService.java:53)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 3 more
Это было полезно?

Решение

SOLVED: I just solved the issue so I'm just answering to help future souls. I was using http-core 4.2-alpha jar and was using 4.2.5 for the rest(http-client, http-client-cache, http-mime etc.). The problem went away after I updated the http-core-4.2-alpha jar to http-core-4.2.5. You can find the jar in the maven repos online.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top