问题
Cant get Nashorn engine
ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
engine.eval("print('Hello World!');");
engine
returns null
I am using eclipse, jdk1.8.0_11
java -version
java version "1.8.0_20-ea"
Java(TM) SE Runtime Environment (build 1.8.0_20-ea-b23)
回答1:
Its working when I pass null param into ScriptEngineManager constructor:
ScriptEngine engine = new ScriptEngineManager(null).getEngineByName("nashorn");
engine.eval("print('Hello World!');");
from java docs
ScriptEngineManager(ClassLoader loader)
If loader is null, the script engine factories that are bundled with the platform and that are in the usual extension directories (installed extensions) are loaded.
回答2:
Old question but in case you didn't have any joy... you might try this instead...
ScriptEngine engine = new NashornScriptEngineFactory().getScriptEngine();
回答3:
Nashorn is Oracle library, so if you have not Oracle Java then you should import it manually.
How to make use of Nashorn
回答4:
This code is correct and work both on Oracle JDK and OpenJDK.
ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
You should use the last version of the JDK 8 (the "ea" version you used is outdated now and probably buggy).
The official documentation of Nashorn is here: http://docs.oracle.com/javase/8/docs/technotes/guides/scripting/nashorn/intro.html#sthref14
Nashorn is an OpenJDK project hosted here: http://openjdk.java.net/projects/nashorn/
来源:https://stackoverflow.com/questions/25332640/getenginebynamenashorn-returns-null