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

这一生的挚爱 提交于 2021-02-08 07:37:22

问题


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.


回答1:


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.




回答2:


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!




回答3:


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



来源:https://stackoverflow.com/questions/2075467/how-to-access-external-jar-files-from-javascript-using-rhino-and-eclipse

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!