javajava-6java-scripting-engine

ScriptEngine with name "nashorn" is null


I'm trying to create ScriptEngine with name "nashorn":

ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("nashorn");
if (engine == null) {
     System.out.println("engine == null");
} 

But I always get

engine == null

Why is this happening? The docs say:

The Nashorn engine is the default ECMAScript (JavaScript) engine bundled with the Java SE Development Kit (JDK).

It means that the Nashorn engine is the default engine and must be present in JDK, don't it?


Solution

  • Here a small snippet to list all supported engines

    public class Script {
        public static void main(String[] args) throws ScriptException {
            new ScriptEngineManager().getEngineByName("js")
               .eval("print('Hello from Java\\n');");
            for (ScriptEngineFactory se : new ScriptEngineManager().getEngineFactories()) {
                System.out.println("se = " + se.getEngineName());
                System.out.println("se = " + se.getEngineVersion());
                System.out.println("se = " + se.getLanguageName());
                System.out.println("se = " + se.getLanguageVersion());
                System.out.println("se = " + se.getNames());
            }
        }
    }
    

    Java 6 (1.6.0_43)

    Hello from Java
    se = Mozilla Rhino
    se = 1.6 release 2
    se = ECMAScript
    se = 1.6
    se = [js, rhino, JavaScript, javascript, ECMAScript, ecmascript]
    

    Java 7 (1.7.0_40)

    Hello from Java                                                  
    se = Mozilla Rhino                                               
    se = 1.7 release 3 PRERELEASE                                    
    se = ECMAScript                                                  
    se = 1.8                                                         
    se = [js, rhino, JavaScript, javascript, ECMAScript, ecmascript] 
    

    Java 8 (1.8.0_74)

    Hello from Java
    
    se = Oracle Nashorn
    se = 1.8.0_74
    se = ECMAScript
    se = ECMA - 262 Edition 5.1
    se = [nashorn, Nashorn, js, JS, JavaScript, javascript, ECMAScript, ecmascript]