How do I get ScriptEngine by engine name?

This example shows how to get an instance of ScriptEngine by the engine name. Below we are trying to obtain the JavaScript ScriptEngine.

package org.kodejava.example.script;

import javax.script.ScriptEngineManager;
import javax.script.ScriptEngine;
import javax.script.ScriptException;

public class GettingScriptEngineByName {
    public static void main(String[] args) {
        ScriptEngineManager manager = new ScriptEngineManager();

        //
        // Obtain an instance of ScriptEngine using the engine name. For
        // example we get a JavaScript ScriptEngine instance.
        //
        ScriptEngine engine = manager.getEngineByName("JavaScript");

        try {
            engine.eval("print('Hello World');");
        } catch (ScriptException e) {
            e.printStackTrace();
        }
    }
}

Wayan

Programmer, runner, recreational diver, live in the island of Bali, Indonesia. Mostly programming in Java, Spring Framework, Hibernate / JPA. Support me by donating.

Leave a Reply