我正在尝试使用名称ScriptEngine
创建"nashorn"
:
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("nashorn");
if (engine == null) {
System.out.println("engine == null");
}
但我总是得到
engine == null
为什么会这样?文档说:
Nashorn引擎是默认的ECMAScript(JavaScript)引擎 与Java SE Development Kit(JDK)捆绑在一起。
这意味着Nashorn引擎是默认引擎,必须存在于JDK中,不是吗?
答案 0 :(得分:4)
这是一个列出所有支持的引擎的小片段
::-webkit-scrollbar {
width: 20px;
height: 20px;
}
::-webkit-scrollbar-thumb {
background-color:#25628e;
}
::-webkit-scrollbar-track {
background-color: #b2c5d9;
border-right: 1px solid rgb(169, 169, 169);
}
Java 6(1.6.0_43)
public class Script {
public static void main(String[] args) throws ScriptException {
new ScriptEngineManager().getEngineByName("js")
.eval("print('Hello from Java\\n');");
for (ScriptEngineFactory se : new ScriptEngineManager().getEngineFactories()) {
System.out.println("se = " + se.getEngineName());
System.out.println("se = " + se.getEngineVersion());
System.out.println("se = " + se.getLanguageName());
System.out.println("se = " + se.getLanguageVersion());
System.out.println("se = " + se.getNames());
}
}
}
Java 7(1.7.0_40)
Hello from Java
se = Mozilla Rhino
se = 1.6 release 2
se = ECMAScript
se = 1.6
se = [js, rhino, JavaScript, javascript, ECMAScript, ecmascript]
Java 8(1.8.0_74)
Hello from Java
se = Mozilla Rhino
se = 1.7 release 3 PRERELEASE
se = ECMAScript
se = 1.8
se = [js, rhino, JavaScript, javascript, ECMAScript, ecmascript]