开发者

HttpClient NoClassDefFoundError

I am trying to run a sample application from HttpClient 4.0.1. It is the file ClientMultiThreadedExecution.java开发者_开发问答 from the examples section. I put in these files in the classpath: apache-mime4j-0.6.jar;commons-codec-1.3.jar;commons-logging-1.1.1.jar;httpclient-4.0.1.jar;httpcore-4.0.1.jar;httpmime-4.0.1.jar and the file compiles correctly. At runtime I get the following error:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/http/client/methods/HttpUriRequest
Caused by: java.lang.ClassNotFoundException: org.apache.http.client.methods.HttpUriRequest
    at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:276)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
    at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)

Am I missing a reference? It seems like a classpath error but I can't figure out which jar file to include? Thank you in advance for your help.


This exception tells that the mentioned class is missing in the runtime classpath.

There are several ways to specify the runtime classpath, depending on how you're executing the program. Since a decent IDE takes this all transparently from your hands, I bet that you're running it in a command prompt.

If you're running it as a JAR file by java.exe -jar or doubleclicking the file, then you need to specify the classpath in the Class-Path entry of the JAR's MANIFEST.MF file. Note that the %CLASSPATH% environment variable and -cp and -classpath arguments are ignored whenever you execute a JAR.

If you're running it as a "plain vanilla" Java application by java.exe, then you need to specify it in the -cp or -classpath argument. Note that whenever you use this argument, the %CLASSPATH% environment variable is ignored.

Either way, the classpath should exist of a (semi)colonseparated string of paths to JAR files (either absolute paths or relative to current working directory). E.g.

java -cp .;/path/to/file1.jar;/path/to/file2.jar com.example.MyClass

(if you're on Unix/Linux, use colon instead of semicolon as path separator)


That class is in httpclient-4.0.1.jar (I've just downloaded it to be sure) so I suspect you haven't put it in the classpath properly.

How are you compiling and running your code?


Running Eclipse Luna 2 (4.4.2) inside cloudera-quickstart-vm-5.8.0 I had to add the following

  • apache-httpcomponents-httpcore.jar
  • httpclient-4.5.3.jar
  • httpclient-cache-4.5.3.jar

... and then it worked without errors


When I experienced this issue, it turned out that when I added the Fluent API as a Maven dependency, it imported a different version of the HTTPClient API than the one I was already using. Both versions of the API were packaged in the resulting JAR's lib folder. The version conflict is what caused this error.

Adding entries to your classpath will fix the problem, because you're just manually specifying what version to use. However, to fix the underlying problem, I just needed to delete my target folder before rebuilding (or run maven clean). This removed any "cached" library JARs, and on the next build, only re-downloaded the correct one.

Hope that helps somebody!


I want to small improve only for Exception when you will problem with connection, sending, ... AND you want to know WAY.

httpclient-4.5.13.jar httpcore-4.5.13.jar +++ for Exception +++ javax.activation-api-1.2.0.jar javax.annotation-api-1.3.2.jar javax.xml.soap-api-1.4.0.jar jaxb-api-2.3.1.jar

public static boolean sendEmailViaExchange( String strUserName, String strPassword, String strDomain, String strURI, String strRecipient, String strSubject, String strMessageBody )  // BYTE attachment
{                   
    try  // e-mail
    {                   
        ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP2);
        service.setCredentials(new WebCredentials(strUserName, strPassword, strDomain));
        service.setUrl( new URI( strURI ) );    

        EmailMessage msg= new EmailMessage(service);                
        msg.setSubject( strSubject );
        msg.setBody(MessageBody.getMessageBodyFromText( strMessageBody ));
        msg.getToRecipients().add( strRecipient );
        msg.send();
    }
    catch (Exception e)     // try  // e-mail   
    {
        LOGGER.log(Level.SEVERE, "ERROR: " + e.getMessage());
                
        return false;
    }   // try  // e-mail  catch (Exception e)
            
    return true;
}   //  public static boolean sendEmailViaExchange( String strUserName, String strPassword, String strDomain, String strURI, String strRecipient, String strSubject, String strMessageBody )  // BYTE attachment
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜