Share this page 

Pass or retrieve values from a scripting engine (jdk1.6)Tag(s): Varia


These examples use the Rhino Javascript engine bundled with Java 6.

The documentation is at Mozilla Rhino site.

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

public class Scripting01 {
  public static void main(String[] args) {
    ScriptEngineManager mgr = new ScriptEngineManager();
    // we are using the rhino javascript engine
    ScriptEngine engine = mgr.getEngineByName("javascript");

    // pass a Java collection to javascript 
    List <String> list1 = Arrays.asList
      ("Homer", "Bart", "Marge", "Maggie", "Lisa");
    engine.put("list1", list1);
    String jsCode = 
      "var index; " 
      + "var values =list1.toArray();"
      + "println('*** Java object to Javascript');"
      + "for(index in values) {" 
      + "  println(values[index]);"
      + "}";
    try {
      engine.eval(jsCode);
    }
    catch (ScriptException se) {
      se.printStackTrace();
    }

    // pass a collection from javascript to java
    jsCode = 
        "importPackage(java.util);"
      + "var list2 = Arrays.asList(['Moe', 'Barney', 'Ned']); ";
    try {
      engine.eval(jsCode);
    }
    catch (ScriptException se) {
      se.printStackTrace();
    }
    List <String> list2 = (List<String>) engine.get("list2");
    System.out.println("*** Javascript object to Java");
    for (String val : list2) {
      System.out.println(val);
    }
  }
}
/*
 output :
    *** Java object to Javascript
    Homer
    Bart
    Marge
    Maggie
    Lisa
    *** Javascript object to Java
    Moe
    Barney
    Ned
*/

The next example uses an external file containing the javascript code. The .JS file is loaded from the classpath.

[scripting02.js]

importPackage(java.util);

var list1 = Arrays.asList
      (['Homer', 'Bart', 'Marge', 'Maggie', 'Lisa']);

var listObject = {
    list2 : Arrays.asList
       (['Moe', 'Barney', 'Ned']),
    getList2 : function() {
       return listObject.list2;
    }
 };  

import java.io.InputStreamReader;
import java.util.List;

import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;

public class Scripting02 {
  public static void main(String[] args) throws Exception{
    ScriptEngineManager mgr = new ScriptEngineManager();
    // we are using the rhino javascript engine
    ScriptEngine engine = mgr.getEngineByName("javascript");

    // the .js is in the classpath
    engine.eval(new InputStreamReader
        (Scripting02.class.getResourceAsStream("scripting02.js")));
    
    // retreive a javascript object directly
    List <String> list1 = (List <String>)engine.get("list1");
    if (list1 != null) {
      for (String s : (List<String>) list1) {
        System.out.println(s);
      }
    }
    System.out.println("***");
    // call a javascript function to retrieve an object
    if (engine instanceof Invocable){
      Invocable engineInv = (Invocable)engine;
      Object obj = engine.get("listObject");
      Object list2 = engineInv.invokeMethod(obj, "getList2");
      if (list2 != null) {
        for (String s : (List<String>) list2) {
          System.out.println(s);
        }
      }
    }
  }
}

/*
   output :
   Homer
   Bart
   Marge
   Maggie
   Lisa
   ***
   Moe
   Barney
   Ned
*/