How do I get a ScriptEngine by language name and version?

This example show you how you can obtain a script engine for a specific language name and specific language version. In the code below we try to obtain script engine instance for ECMAScript version ECMAScript 262 Edition 11.

package org.kodejava.script;

import javax.script.ScriptEngineManager;
import javax.script.ScriptEngineFactory;
import javax.script.ScriptEngine;
import javax.script.ScriptException;
import java.util.List;

public class ScriptEngineSearch {
    public static void main(String[] args) {
        String languageName = "ECMAScript";
        String languageVersion = "ECMAScript 262 Edition 11";

        // Creating a ScriptEngineManager and get the list of available
        // engine factories.
        ScriptEngineManager manager = new ScriptEngineManager();
        List<ScriptEngineFactory> factories = manager.getEngineFactories();

        // We obtain a ScriptEngine from the available factories where
        // the language name is "ECMAScript" and the version is
        // "ECMAScript 262 Edition 11". ECMAScript is the standard name
        // for JavaScript programming language.
        //
        // If we found the desired language we then get the ScriptEngine
        // by calling factory's getScriptEngine() method.
        ScriptEngine engine = null;
        for (ScriptEngineFactory factory : factories) {
            String language = factory.getLanguageName();
            String version = factory.getLanguageVersion();

            if (language.equals(languageName)
                    && version.equals(languageVersion)) {
                engine = factory.getScriptEngine();
                break;
            }
        }

        if (engine != null) {
            try {
                engine.eval("print('Hello There')");
            } catch (ScriptException e) {
                e.printStackTrace();
            }
        }
    }
}

Maven Dependencies

<dependencies>
    <dependency>
        <!-- https://search.maven.org/remotecontent?filepath=org/graalvm/js/js/22.2.0/js-22.2.0.jar -->
        <groupId>org.graalvm.js</groupId>
        <artifactId>js</artifactId>
        <version>22.2.0</version>
    </dependency>
    <dependency>
        <!-- https://search.maven.org/remotecontent?filepath=org/graalvm/js/js-scriptengine/22.2.0/js-scriptengine-22.2.0.jar -->
        <groupId>org.graalvm.js</groupId>
        <artifactId>js-scriptengine</artifactId>
        <version>22.2.0</version>
    </dependency>
</dependencies>

Maven Central Maven Central

How do I get the supported scripting engine?

package org.kodejava.script;

import javax.script.ScriptEngineManager;
import javax.script.ScriptEngineFactory;
import java.util.List;

public class GetSupportedScriptingEngine {
    public static void main(String[] args) {
        // Creating an instance of ScriptEngineManager an get the list
        // of available ScriptEngineFactory.
        ScriptEngineManager manager = new ScriptEngineManager();
        List<ScriptEngineFactory> factories = manager.getEngineFactories();

        for (ScriptEngineFactory factory : factories) {
            System.out.println(
                    "EngineName      = " + factory.getEngineName());
            System.out.println(
                    "EngineVersion   = " + factory.getEngineVersion());
            System.out.println(
                    "LanguageName    = " + factory.getLanguageName());
            System.out.println(
                    "LanguageVersion = " + factory.getLanguageVersion());
            System.out.println(
                    "Extensions      = " + factory.getExtensions());
            System.out.println();

            List<String> names = factory.getNames();
            for (String name : names) {
                System.out.println("Engine Alias = " + name);
            }
        }
    }
}

The code above produces the following information.

EngineName      = Graal.js
EngineVersion   = Development Build
LanguageName    = ECMAScript
LanguageVersion = ECMAScript 262 Edition 11
Extensions      = [js, mjs]

Engine Alias = js
Engine Alias = JS
Engine Alias = JavaScript
Engine Alias = javascript
Engine Alias = ECMAScript
Engine Alias = ecmascript
Engine Alias = Graal.js
Engine Alias = graal.js
Engine Alias = Graal-js
Engine Alias = graal-js
Engine Alias = Graal.JS
Engine Alias = Graal-JS
Engine Alias = GraalJS
Engine Alias = GraalJSPolyglot

Maven Dependencies

<dependencies>
    <dependency>
        <!-- https://search.maven.org/remotecontent?filepath=org/graalvm/js/js/22.2.0/js-22.2.0.jar -->
        <groupId>org.graalvm.js</groupId>
        <artifactId>js</artifactId>
        <version>22.2.0</version>
    </dependency>
    <dependency>
        <!-- https://search.maven.org/remotecontent?filepath=org/graalvm/js/js-scriptengine/22.2.0/js-scriptengine-22.2.0.jar -->
        <groupId>org.graalvm.js</groupId>
        <artifactId>js-scriptengine</artifactId>
        <version>22.2.0</version>
    </dependency>
</dependencies>

Maven Central Maven Central