我正在使用 Mozilla Rhino 编写 JavaScript 服务器应用程序。我想在我的项目中包含 HttpClient 类以轻松访问网络,但我不知道如何配置我的 Eclipse 项目以使 Rhino 加载 HttpClient JAR 文件。
我已将 js.jar
(来自 Rhino)和 httpclient-4.0.1.jar
添加到我在 Eclipse 中的项目构建路径,并且在我的运行配置中我有从 Rhino 的 js.jar(它找到的)指定 Main 类,在我的 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.
怎么会这么难呢?我一定是在做一些根本性的错误。
最佳答案
如果您的类路径设置正确,我不明白为什么您的代码不能工作。我建议尝试显式加载该类以查看它是否真的可用,否则您似乎在类路径中没有它:
println( java.lang.Class.forName( 'org.apache.http.client.methods.HTTPGet' ) );
Rhino 应该能够毫无问题地从外部 jar 文件加载到类。
关于java - 如何使用 Rhino 和 Eclipse 从 JavaScript 访问外部 JAR 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2075467/