如何使用 Rhino 和 Eclipse 从 JavaScript 访问外部 JAR 文件?
我正在使用 Mozilla Rhino 编写 JavaScript 服务器应用程序。我想在我的项目中包含 HttpClient 类以轻松访问网络,但我不知道如何配置我的 Eclipse 项目以使 Rhino 加载 HttpClient JAR 文件。
我已将 js.jar
(来自 Rhino)和 httpclient-4.0.1.jar
添加到 Eclipse 中项目的构建路径中,并在我的运行配置中指定了 Main从 Rhino 的 js.jar(它找到的)中获取类,在我的 JavaScript 文件中我基本上这样做:
importPackage(org.apache.http.client.methods);
var get = new HttpGet("<some url returning json data>");
get.execute();
print(get.getResponseBodyAsString());
它没有说:
Exception in thread "Thread-0" org.mozilla.javascript.EcmaError: ReferenceError: "HttpGet" is not defined.
这怎么这么难?我一定是做了一些根本性错误的事情。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当您运行程序时,您是否确保
httpclient-4.0.1.jar
位于类路径中?Have you made sure that
httpclient-4.0.1.jar
is in the class path when you run the program?我不明白为什么如果你的类路径设置正确,你的代码就不能工作。我建议尝试显式加载该类以查看它是否确实可用,否则您的类路径中似乎没有它:
Rhino 应该能够从外部 jar 文件加载类,而不会出现问题或额外的工作。
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:
Rhino should be able to load to classes from external jar files without problems or extra work.
好的,问题似乎是我在类路径和项目的构建路径中都有多个
httpclient-4.0.1.jar
文件的引用。除此之外,我似乎遗漏了httpclient-4.0.1.jar
所依赖的几个依赖项。清理我的类路径并将所有必要的 jar 添加到我的项目后,现在似乎找到了预期的一切。谢谢你们的帮助!
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 thathttpclient-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!