java examples
Home Java Examples Resources Java Interview Questions
Brainpower SEO

How to get the supported scripting engine?

package com.javacoderanch.example.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());

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

The code above produces the following information.

EngineName      = Mozilla Rhino
EngineVersion   = 1.6 release 2
LanguageName    = ECMAScript
LanguageVersion = 1.6
Extensions      = [js]
Engine Alias = js
Engine Alias = rhino
Engine Alias = JavaScript
Engine Alias = javascript
Engine Alias = ECMAScript
Engine Alias = ecmascript

Currently, as you can see in the result of the program above, Mozilla Rhino is the only scripting engine provided in the JDK 6 libraries. You can also see that the factory provides many engine name aliases that help you to obtain an engine for the JavaScript programming language.