How do I import Java package in script?

Here you can see how to import a Java class so that you can use the class, creates an instance of
it in the scripting environment. We want to print out the current date on the console. For this we
need to import the Date class that’s packaged under the java.util package.

In the script we can import the java.util package using the following script
importPackage(java.util).

package org.kodejava.example.script;

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

public class ImportPackageExample {
    public static void main(String[] args) {
        ScriptEngineManager manager = new ScriptEngineManager();
        ScriptEngine engine = manager.getEngineByExtension("js");

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

    private static String getScript() {
        StringBuilder sb = new StringBuilder();
        sb.append("importPackage(java.util);");
        sb.append("");
        sb.append("var today = new Date();");
        sb.append("println('Today is ' + today);");
        return sb.toString();
    }
}

This program prints the following line:

Today is Fri Sep 11 2009 17:03:02 GMT+0800 (CST)

How do I modified Java object in script?

This example show how to modified Java object from scripting environment. Below we manipulate a collection of string data. To pass data into the scripting engine we use a key-value binding to the script engine.

package org.kodejava.example.script;

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

public class ModifiedJavaObjectFromScript {
    public static void main(String[] args) {
        //
        // Creating an array of five colors
        //
        List colors = new ArrayList();
        colors.add("White");
        colors.add("Black");
        colors.add("Red");
        colors.add("Green");
        colors.add("Blue");
        
        //
        // Obtain a ScriptEngine instance.
        //
        ScriptEngineManager manager = new ScriptEngineManager();
        ScriptEngine engine = manager.getEngineByExtension("js");

        //
        // Place the colors list into the engine using colorList key.
        // After setting the list into the engine our script will be
        // able to read it.
        //
        engine.put("colorList", colors);
        
        try {
            engine.eval(getScript());
            
            //
            // Redisplay the modified version of colors list object.
            //
            for (String color : colors) {
                System.out.println("Color = " + color);
            }
        } catch (ScriptException e) {
            e.printStackTrace();
        }
    }

    private static String getScript() {
        //
        // Below is our script to read the values of Java List that
        // contains string of colors. We also add some other colors
        // to the list object in the script environment.
        //
        String script =
                "var index; " +
                "var colors = colorList.toArray(); " +
                " " +
                "for (index in colors) { " +
                "    println(colors[index]); " +
                "}" +
                " " +
                "colorList.add("Yellow"); " +
                "colorList.add("Purple"); " +
                "colorList.add("Orange"); ";
        return script;
    }
}

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