views:

167

answers:

2

I am using Netbeans IDE for a java project. In this project i need a jar file "htmlunit-2.6.jar".

I have included this jar file in the project libraries folder. I have instantiated one of its class "WebClient" but this class needs other classes of "commons-httpclient-3.1.jar" file.

Now I have also included "commons-httpclient-3.1.jar" file in the project libraries folder. But when I compiled my source file, it throws

ClassNotFoundException: org.apache.commons.httpclient.auth.CredentialsProvider

Kindly tell me how to handle this situation when one class in one jar file needs other classes in other jar file.

+5  A: 

Simply put the required jar files on the classpath at compile-time and it should work. If you were doing it from the command-line then it would look like this:

javac -cp jar1:jar2 my.Application

If you are using NetBeans then you need to tell NetBeans that both of the JARs are on your classpath. It will be definable in a Project > Properties wizard as described here and also here from the tutorial

oxbow_lakes
A: 

The ClassNotFoundException tells you that your libraries have some dependencies that you don't have included yet in your classpath at runntime. Your source is well, because if you have used something not available, NB will tell you this at compile time (or before when editing).

So, welcome in the "dependency hell" of Java. For small proyects you will be able to check all dependencies by hand with readme files, docs, etc and put them in the proyect config as oxbow_lakes said. For bigger things look at maven. It will do (most) everything for you !

(Maven is available in NB6)

PeterMmm
Not adding **one JAR** to the classpath is not *quite* "dependency hell"!
oxbow_lakes
As i understand OP he is talking at this moment about 2 Jars and the program does not run. Who knows how many will jars will need the running version ...
PeterMmm
Yes I see. 2 jars - that's twice as bad which, in dog years, means 2147483647% worse
oxbow_lakes