4

I get next error:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory at org.apache.http.conn.ssl.DefaultHostnameVerifier.(DefaultHostnameVerifier.java:82) at org.apache.http.impl.client.HttpClientBuilder.build(HttpClientBuilder.java:955) at Main.main(Main.java:87) Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory at java.net.URLClassLoader.findClass(URLClassLoader.java:381) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:338) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ... 3 more

I use intellij idea and imported 2 libs: httpclient-4.5.5.jar and httpcore-4.4.9.jar

All libs in my class:

import org.apache.http.HttpHost;
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;

And code sample:

    String urlToSendRequest = Constants.HOST + Constants.URL;
    String targetDomain = Constants.DOMAIN;

    HttpClient httpClient = HttpClientBuilder.create().build();
    HttpHost targetHost = new HttpHost(targetDomain, 80, "http");

    HttpPost httpPost = new HttpPost(urlToSendRequest);

    httpPost.addHeader("SENDCODE", "UTF-8");
    //...

    StringEntity entity = new StringEntity(Constants.MSG, "UTF-8");
    entity.setContentType("application/xml");
    httpPost.setEntity(entity);

    HttpResponse response = httpClient.execute(httpPost);

I'm almost sure that the problem is with libraries importing, but I'm not sure and have no idea how to fix it.

JohnA
  • 335
  • 1
  • 3
  • 12
  • 1
    Try adding `commons-logging` to your classpath. – lexicore Apr 04 '18 at 09:06
  • Looks like u are missing Apache Commons libs , please improt those. https://commons.apache.org/proper/commons-logging/ – Rehan Azher Apr 04 '18 at 09:07
  • Duplicate: https://stackoverflow.com/questions/1776415/apache-httpclient-throws-a-noclassdeffounderror – VaL Apr 04 '18 at 09:09
  • possible duplicate of https://stackoverflow.com/questions/1551697/getting-java-lang-classnotfoundexception-org-apache-commons-logging-logfactory – DAIRAV Apr 04 '18 at 09:12
  • I'd love an answer that doesn't say "Then add it..." I CAN'T add it due to company policies here - what can I do?! – Dan Rayson Nov 12 '18 at 13:57

3 Answers3

2

Please include commons logging jar in your project. Can be downloaded from here

Also from Maven repository

DAIRAV
  • 723
  • 1
  • 9
  • 31
1

Seems like you are missing commons-logging lib.

doubleW
  • 590
  • 5
  • 17
0

please download the commons-logging jar file and set the path.

http://commons.apache.org/proper/commons-logging/download_logging.cgi

shrikant
  • 441
  • 5
  • 11
  • Thank you. it's really strangely, because I have already try this, but now it's works. Thank you again. – JohnA Apr 04 '18 at 09:15