Skip to content

Instantly share code, notes, and snippets.

@pborissow
Last active May 20, 2023 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pborissow/a1d8a7721d131b773235cca88dc0b88c to your computer and use it in GitHub Desktop.
Save pborissow/a1d8a7721d131b773235cca88dc0b88c to your computer and use it in GitHub Desktop.
Nashorn Notes

Nashorn used to be bundled with Java between versions 8 to 14. It no longer ships starting with Java 15 and needs to be downloaded as a seperate JAR via OpenJDK.

https://github.com/openjdk/nashorn

Migrating to the OpenJDK version of Nashorn is pretty straightforward.

Old Way

//Scripting includes
import javax.script.*;
import jdk.nashorn.api.scripting.ScriptObjectMirror;

//Get Scripting Engine
ScriptEngineManager factory = new ScriptEngineManager();
ScriptEngine engine = factory.getEngineByName("nashorn");

New Way

//Scripting includes
import javax.script.*;
import org.openjdk.nashorn.api.scripting.ScriptObjectMirror;
import org.openjdk.nashorn.api.scripting.NashornScriptEngineFactory;
import org.openjdk.nashorn.api.scripting.NashornScriptEngine;


//Get Scripting Engine
String[] options = new String[] { "--language=es6" };
NashornScriptEngineFactory factory = new NashornScriptEngineFactory();
NashornScriptEngine engine = (NashornScriptEngine) factory.getScriptEngine(options);

Dependencies

At the time of this writing, the most current OpenJDK version of Nashorn is 15.4 and has the following dependencies: image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment