Skip to content

Instantly share code, notes, and snippets.

Created June 2, 2022 14:39
What would you like to do?
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.

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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment