getEngineByName("nashorn") returns null
Asked Answered
V

4

10

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)

Venusberg answered 15/8, 2014 at 19:6 Comment(0)
V
21

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.

Venusberg answered 15/8, 2014 at 19:22 Comment(0)
S
5

Old question but in case you didn't have any joy... you might try this instead...

ScriptEngine engine = new NashornScriptEngineFactory().getScriptEngine();
Stumble answered 3/6, 2015 at 16:46 Comment(1)
Thanks, @Marrow父, this solution did help in my case.Alcoholic
T
1

Nashorn is Oracle library, so if you have not Oracle Java then you should import it manually.

How to make use of Nashorn

Tessera answered 15/8, 2014 at 19:14 Comment(1)
I would give a try, Java is backward-compatible in most casesTessera
O
0

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/

Outlying answered 22/12, 2015 at 10:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.