views:

585

answers:

3

Hi,

I'm using Mozilla Rhino to write a JavaScript server application. I would like to include the HttpClient classes in my project to easily access the web, but I can't figure out how to configure my Eclipse project to get Rhino to load the HttpClient JAR file.

I have added js.jar (from Rhino) and httpclient-4.0.1.jar to my project's build path in Eclipse, and in my run configuration I have specified the Main class from Rhino's js.jar (which it finds), and in my JavaScript file I basically do this:

importPackage(org.apache.http.client.methods);
var get = new HttpGet("<some url returning json data>");
get.execute();
print(get.getResponseBodyAsString());

It fails saying this:

Exception in thread "Thread-0" org.mozilla.javascript.EcmaError: ReferenceError: "HttpGet" is not defined.

How can this be so hard? I must be doing something fundamentally wrong.

A: 

Have you made sure that httpclient-4.0.1.jar is in the class path when you run the program?

Matthew Crumley
Well what's the difference between build path and classpath? I assume build path is at compile time (or something) and classpath is at runtime? I've added my `httpclient-4.0.1.jar` to a classpath folder, and if I write pure Java (instead of JavaScript and run it through Rhino), it finds the HttpClient library perfectly.
vrutberg
+1  A: 

I don't see why your code shouldn't work if your classpath is set up correctly. I would suggest to try to load the class explicitly to see if it is really available, otherwise you don't seem to have it in you classpath:

println( java.lang.Class.forName( 'org.apache.http.client.methods.HTTPGet' ) );

Rhino should be able to load to classes from external jar files without problems or extra work.

x4u
+1  A: 

Okay, the problem seemed to be that I had multiple references of the httpclient-4.0.1.jar file, both in my classpath and in my project's build path. Other than that, I seemed to have left out a couple of dependencies that httpclient-4.0.1.jar depend upon. After cleaning out my classpath and adding all the necessary jars to my project it now seems to find everything as expected.

Thanks for your help guys!

vrutberg