从脚本调用 javascript 解释器
我在Rhino 1.7下用Javascript编写了一些脚本,其中一个启动一个最小的http服务器并接受输入中的JS命令。
现在,如果我调用(从Rhino内部):
engine = ScriptEngineManager().getEngineByName("JavaScript");
我得到内置的JS引擎(来自Java 1.6),这是Rhino的旧版本,并且缺少一些功能(例如用于多个接口的JavaAdapter)。
我如何获得 Rhino 引擎而不是它?我需要 ScriptEngineManager.getEngineFactories() 还是其他什么?
I've written some scripts in Javascript under Rhino 1.7, one of them starts a minimal http server and accepts JS commands in input.
Now, if I call (from within Rhino):
engine = ScriptEngineManager().getEngineByName("JavaScript");
I get the builtin JS engine (from Java 1.6), that is an older version of Rhino, and lacks some functions (like JavaAdapter for multiple interfaces).
How do I get the Rhino Engine instead of that? Do I need ScriptEngineManager.getEngineFactories() or what else?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您想要实现的是选择实现“JavaScript”的脚本引擎的某个版本。正确的方法是调用
ScriptEngineManager.getEngineFactories()
,然后检查getLanguageName()
和getEngineVersion()
的结果。What you want to achieve is to select a certain version of an script engine which implements "JavaScript". The correct way to do that is to call
ScriptEngineManager.getEngineFactories()
and then check the results ofgetLanguageName()
andgetEngineVersion()
.我自己发现了这一点(反复试验)。如上所述,Rhino 没有注册引擎工厂。您可以获得当前引擎(作为上下文和可编写脚本的对象):
使用这些对象,我可以使用 evalString/evalReader 运行我的脚本或命令行。
I found it out myself (trial and error). As noted above, Rhino doesn't register an engine factory. You can get the current engine (as a context and a scriptable object):
With these objects, I can run my scripts or command lines using evalString/evalReader.
在调用初始脚本之前,为什么不在脚本内将正在使用的引擎设置为上下文变量呢?这样,在脚本内部,您就可以访问运行它的引擎。
Before invoking your initial script, why don't you set the engine you're using as a context variable inside the script? That way, inside the script, you'll have access to the engine that is running it.