Hello Nashorn

suggest change

Nashorn was included in Java 8 through 14 and removed in Java 15. getEngineByName returns null when the engine is not installed, and calling eval on that null throws NullPointerException rather than ScriptException.

// Obtain an instance of the Nashorn JavaScript engine
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("nashorn");

// Execute a hardcoded script
try {
    engine.eval("print('Hello Nashorn!');");
} catch (ScriptException ex) {
    // Checked exception declared by ScriptEngine.eval
    ex.printStackTrace();
}

// Outcome:
// 'Hello Nashorn!' printed on standard output

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents