Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 611
user389753 Avatar asked Jul 12 '10 18:07

user389753


People also ask

How do you fix No Class Def Found error?

lang. NoClassDefFoundError, which means the Class Loader file responsible for dynamically loading classes can not find the . class file. So to remove this error, you should set your classpath to the location where your Class Loader is present.

Does HttpClient need to be closed?

You do not need to explicitly close the HttpClient, however, (you may be doing this already but worth noting) you should ensure that connections are released after method execution. Edit: The ClientConnectionManager within the HttpClient is going to be responsible for maintaining the state of connections.

What is NTCredentials?

@Contract(threading=IMMUTABLE) public class NTCredentials extends Object implements Credentials, Serializable. Microsoft Windows specific Credentials representation that includes Windows specific attributes such as name of the domain the user belongs to. Since: 4.0 See Also: Serialized Form.

What is the use of HttpComponentsClientHttpRequestFactory?

HttpComponentsClientHttpRequestFactory is ClientHttpRequestFactory implementation that uses Apache HttpComponents HttpClient to create requests. We have used @Scheduled annotation in httpClient configuration. To support this, we have to add support of scheduled execution of thread.


2 Answers

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)

like image 178
BalusC Avatar answered Oct 02 '22 04:10

BalusC


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?

like image 31
Jon Skeet Avatar answered Oct 02 '22 04:10

Jon Skeet