开发者

How to access external JAR files from JavaScript using Rhino and Eclipse?

开发者 https://www.devze.com 2022-12-16 08:34 出处:网络
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 Ec

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 con开发者_JAVA百科figuration 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.


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.


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!


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

0

精彩评论

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

关注公众号