How do I access Java object from a script?

This example demonstrate how you can access Java object from a script. We put Java object into the script engine by calling the ScriptEngine‘s put(String key, Object value) method. This value can later be read or access by our script. For example we pass an array of string and a date object for our script to display.

package org.kodejava.example.script;

import javax.script.ScriptEngineManager;
import javax.script.ScriptEngine;
import javax.script.ScriptException;
import java.util.Date;

public class AccessJavaObjectFromScript {
    public static void main(String[] args) {
        //
        // Creating an array of five colors
        //
        String[] colors = {"White", "Black", "Red", "Green", "Blue"};
        Date now = new Date();

        //
        // Below is our script to read the values of Java array that
        // contains string of colors.
        //
        String script =
                "var index; " +
                "var colors = colorArray; " +
                " " +
                "for (index in colors) { " +
                "    println(colors[index]); " +
                "}" +
                "println('----------'); " +
                "println('Today is ' + date); ";

        //
        // Obtain a ScriptEngine instance.
        //
        ScriptEngineManager manager = new ScriptEngineManager();
        ScriptEngine engine = manager.getEngineByExtension("js");

        //
        // Place the colors array into the engine using colorArray key.
        // After setting the array into the engine our script will be
        // able to read it.
        //
        engine.put("colorArray", colors);
        engine.put("date", now);

        try {
            engine.eval(script);
        } catch (ScriptException e) {
            e.printStackTrace();
        }
    }
}

Our code will print as follow:

White
Black
Red
Green
Blue
----------
Today is Mon Jul 13 23:12:19 SGT 2009

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 1.6.

package org.kodejava.example.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 = "1.6";

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

        //
        // We obtain a ScriptEngine from the available factories where
        // the language name is ECMAScript and the version is 1.6.
        // 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();
            }
        }
    }
}