0

I've been reading about this error and I can't see where I went wrong. I have the org.apache.httpcomponents.httpclient_4.5.2.jar added to the project libraries, the run libraries, and compile libraries.

This is my source code:

    public void HttpFunction()
{
    try
    {
        String url = "http://www.google.com";
        CloseableHttpClient client = HttpClients.createDefault();
        HttpGet web_target = new HttpGet(url);
        ResponseHandler<String> response_handler = new BasicResponseHandler();
        String web_response = client.execute(web_target, response_handler);
        System.out.println(web_response);
        client.close();
    }
    catch (Exception no)
    {
        System.out.println("ERROR: " + no);
    }
}

I do have all of the required imports and they are recognized. This is the gargantuan list of errors it creates:

java.lang.NoClassDefFoundError: org/apache/http/HttpRequest
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2615)
at java.lang.Class.getMethod0(Class.java:2856)
at java.lang.Class.getMethod(Class.java:1668)
at sun.launcher.LauncherHelper.getMainMethod(LauncherHelper.java:494)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:486)
Caused by: java.lang.ClassNotFoundException: org.apache.http.HttpRequest
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 18 more
Exception in thread "main" C:\Users\JOE_LAPTOP\AppData\Local\NetBeans\Cache\8.1\executor-snippets\run.xml:53: Java returned: 1

BUILD FAILED (total time: 0 seconds)

I am completely lost here after days of research and trial and error. Please help.

Joe Q
  • 16
  • 5
  • That class is part of httpcore, not httpclient. The httpclient maven (are you using maven?) dependency has a compile dependency on httpcore, but that's it. – Sotirios Delimanolis May 09 '16 at 00:52
  • Are you saying that I should use a different method of constructing a client for httpget, or that I have my imports incorrect? I think you're suggesting the ladder, but I'm not sure. Also, could you please link the duplicate? @SotiriosDelimanolis – Joe Q May 10 '16 at 01:43
  • Refresh the page. The link is at the top. I'm saying you're missing a library (`httpcore`) on the class path at runtime. – Sotirios Delimanolis May 10 '16 at 01:45
  • I've added org.apache.httpcomponents.httpclient_4.5.2.jar to the run and compile class paths. I don't understand. I searched Apache for httpcore and I didn't find another file that I was missing or anything. I did find something in the question you linked me too that said: set classpath=axis.jar;%classpath%;. If that were to work, where would I place it? @SotiriosDelimanolis – Joe Q May 11 '16 at 01:31
  • Those are system properties. How are you launching your `java` process? You should have both those `jar`s (and any other) in the classpath listed by `-cp`. – Sotirios Delimanolis May 11 '16 at 01:32

0 Answers0