java - How to access external JAR files from JavaScript using Rhino and Eclipse? - Stack Overflow

I'm using Mozilla Rhino to write a JavaScript server application. I would like to include the Http

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.

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.

Share Improve this question asked Jan 15, 2010 at 23:37 vrutbergvrutberg 2,0481 gold badge21 silver badges30 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

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?

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745224170a4617384.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信