我正在尝试从 jdk.nashorn.*
命名空间直接初始化 Oracle 的 javascript nashorn 引擎。
(nashorn 库是 2013 年 1 月的 beta 版本)。
有一个 Web 示例,它使用 javax.script.ScriptEngineManager 实用程序类调用引擎的 Nashorn 引擎实例。
var engine = ScriptEngineManager.getEngineByName(*)
但是,我喜欢远离 ScriptEngineManager,所以我需要像 Rhino 一样直接调用引擎。
Context cx = Context.enter();
Scriptable scope = cx.initStandardObjects();
如何直接创建nashorn引擎实例?
最佳答案
javax script engine by type application/javascript Hashorn,返回一个脚本引擎并告诉它做一些事情,它还提供可调用和可编译的接口(interface)。
您可能有兴趣阅读此内容:How can I start coding with Oracle's Nashorn JS Engine and when will it replace Rhino in the OpenJDK?
示例用法:
import javax.*; //lib imports
// we should use the javax.script API for Nahsorn
ScriptEngineManager m = new ScripteEngineManager();
ScriptEngine e = m.getEngineByname("nashorn");
try {
e.eval("print('hello nashorn !')");
} catch(Exception e) {
// using jdk lower then version 8 maybe ?
}
关于javascript - 如何直接初始化Nashorn Context和engine?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14952891/