Get "ClassNotFoundException when using HttpClientBuilder

Old programmer, new to Java. I am trying to run what, in my opinion, is a fairly common code example, which is similar in a number of places on the Internet, HttpClient httpClient = HttpClientBuilder.create().build()throws an exception, and I cannot understand why. I am using HttpClient 4.3.

import java.io.IOException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

public class ATest{

   public static void main(String[] args) throws Exception {
      String strURL  = "http://192.9.10.11/cgi-bin/echo";
      String message = "hello world";
      // next line throwsClassNotFoundException, why?
      HttpClient httpClient = HttpClientBuilder.create().build();
      HttpPost   httpPost   = new  HttpPost(strURL);
      httpPost.setEntity(new StringEntity(message));
      HttpResponse response = httpClient.execute(httpPost);
      try {
         System.out.println(response.getStatusLine());
         HttpEntity entity = response.getEntity();
         // do something useful with the response body
         // and ensure it is fully consumed
         EntityUtils.consume(entity);
      } finally {
         response.close();
      }
   }
}
+4
source share
1 answer

Java VM comes with a lot of classes, but not org.apache.http.*.

You need to help the Java virtual machine, for example, you will help gcc to associate binary code in C or C ++ with -lxxxand LD_LIBRARY_PATHwith the concept of classpath. java -cp <path>:<path>:<path>indicate where classes are needed (e.g. .so for binaries on Unix).

Classes

org.apache.http.* . jar cp <path> spec.

, apache http client 4.3 delivery, lib:

  • HttpClient-4.3.jar
  • httpmime-4.3.jar
  • --4.3.jar
  • HttpClient--4.3.jar
  • httpcore-4.3.jar
  • - 1.1.3.jar
  • --1.6.jar

, , commons-logging-1.1.3.jar httpclient-4.3.jar

+6

All Articles