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)

Artur
  • 327
  • 2
  • 14

4 Answers4

20

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.

Artur
  • 327
  • 2
  • 14
4

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

ScriptEngine engine = new NashornScriptEngineFactory().getScriptEngine();
Marrow父
  • 357
  • 2
  • 6
1

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

How to make use of Nashorn

Scadge
  • 9,380
  • 3
  • 30
  • 39
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/

Emmanuel Keller
  • 3,384
  • 1
  • 14
  • 16