如何使用 Rhino 和 Eclipse 从 JavaScript 访问外部 JAR 文件?

发布于 2024-08-17 16:08:05 字数 672 浏览 6 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

倥絔 2024-08-24 16:08:06

当您运行程序时,您是否确保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?

孤城病女 2024-08-24 16:08:05

我不明白为什么如果你的类路径设置正确,你的代码就不能工作。我建议尝试显式加载该类以查看它是否确实可用,否则您的类路径中似乎没有它:

println( java.lang.Class.forName( 'org.apache.http.client.methods.HTTPGet' ) );

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:

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.

浅听莫相离 2024-08-24 16:08:05

好的,问题似乎是我在类路径和项目的构建路径中都有多个 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 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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文