0

I have encountered an error that seems pretty common. Tried pretty much ALL the solutions recommended on here and some from other sites. Still no luck. I am using Eclipse Oxygen. I get this error on running. I am using a JAR which I have added to the project's lib folder (hence the import statement - which resolves fine). Any help would be greatly appreciated.

code:

package com.ed;
import microsoft.exchange.webservices.data.core.ExchangeService;

public class Test {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ExchangeService service = new ExchangeService();
    }

}

Error:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/http/conn/HttpClientConnectionManager
    at com.ed.Test.main(Test.java:8)
Caused by: java.lang.ClassNotFoundException: org.apache.http.conn.HttpClientConnectionManager
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 1 more
  • possible duplicate of https://stackoverflow.com/questions/34413/why-am-i-getting-a-noclassdeffounderror-in-java – Ahmed Ashour Jan 06 '18 at 21:27
  • I have tried the solutions recommend on this site as stated. – David Jones Jan 06 '18 at 21:29
  • 4
    You can't just "try the solutions". You have to understand the problem, only then you can start to fix it. The problem is always the same: the class is not on the runtime classpath. – Kayaman Jan 06 '18 at 21:34

2 Answers2

1

Not sure which jar file you are using, maybe something similar to: https://mvnrepository.com/artifact/com.microsoft.ews-java-api/ews-java-api/2.0

If you are, then it has dependencies on other jars that must be included. The are listed in the link above. One of those is is httpclient: https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient/4.4.1. Eclipse will not automatically download dependent jars. Build tools like maven or gradle will do that for you.

Neil Hart
  • 61
  • 1
  • 5
0

This is due to Class not found, you can print all the JARs used by the application by something like:

for (String entry : System.getProperty("java.class.path").split(File.pathSeparator)) {
    System.out.println(entry);
}

You must have a JAR which contains the class in question, i.e. httpclient, as hinted here.

Sometimes, you can have conflicting versions as well.

Sometimes, the JAR is not fully downloaded, which can be checked by manually opening the JAR (as .zip), and see its contents.

Ahmed Ashour
  • 5,179
  • 10
  • 35
  • 56
  • Yes, I have a web service oriented JAR (hence HttpClient). That JAR is the one I have added. – David Jones Jan 06 '18 at 21:34
  • Open the jar manually and check if it really has the needed class, inside the correct folder, sometimes JARs are incompletely downloaded. – Ahmed Ashour Jan 06 '18 at 21:35
  • What is best way to view classes within, too many packages to expand – David Jones Jan 06 '18 at 21:39
  • It doesn't resolve when added as import statement so not there, right? – David Jones Jan 06 '18 at 22:03
  • So I need to find fixed JAR version? – David Jones Jan 06 '18 at 22:05
  • You need to all JARs, even if you indirectly reference them (as in this example), so you use class A, which internally uses Class B, you need to all JARs for both A and B, in the delivery of `ExchangeService`, you should find all dependency JARS (or a reference to them at least) – Ahmed Ashour Jan 06 '18 at 22:07
  • I guess you are using the one from [here](https://github.com/OfficeDev/ews-java-api/blob/master/src/main/java/microsoft/exchange/webservices/data/core/ExchangeService.java), which has a version of [4.4.1](https://github.com/OfficeDev/ews-java-api/blob/master/pom.xml) – Ahmed Ashour Jan 06 '18 at 22:10