DefaultHttpClient throws Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory

472 views Asked by At

I have a standalone OAuth2.0 java program. I am using

DefaultHttpClient client = new DefaultHttpClient()

in my code and I have external Jar httpClient jar added to my referenced libraries. But when I run my class, it shows the below error:

Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory at java.net.URLClassLoader.findClass(URLClassLoader.java:381) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331‌​) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ... 8 more

public class OAuth {
    public static void main(String[] args) {
        String resourceURL       = "google.com";
        HttpPost post            = new HttpPost(resourceURL);
        DefaultHttpClient client = new DefaultHttpClient();
        HttpResponse response    = null;
        try {
            response = client.execute(post);
        } catch (ClientProtocolException e) { 
           // TODO Auto-generated catch block 
           e.printStackTrace(); 
        } catch (IOException e) {  
           // TODO Auto-generated catch block 
           e.printStackTrace(); 
        } finally { post.releaseConnection(); 
    } 
    ---print something 
    } 
}
2

There are 2 answers

0
periwinkle On BEST ANSWER

Try to use a compatible log4j version, your version is higher than required. You may use log4j 1.2.16, since your slf4j-log4j12 version is 1.6.1.

3
ShaharT On

Just make sure slf4j-api/log4j jar is on your classpath