getEngineByName(“nashorn”) returns null

怎甘沉沦 提交于 2019-12-06 22:32:16

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!