Old programmer, new to Java. I am trying to run what I think is pretty common example code that is similar in a number of places on the web, HttpClient httpClient = HttpClientBuilder.create().build() throws an exception and I can't figure out why. I'm 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();
}
}
}
ClassNotFoundExceptionoccurs when your application classpath does not contain the specified class. How are you running your application?