Pass or retrieve values from a scripting engine (jdk1.6)
import java.util.Arrays;
import java.util.List;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
public class Main {
public static void main(String[] args) throws Exception{
ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine engine = mgr.getEngineByName("javascript");
List<String> list1 = Arrays.asList("A", "B", "C", "D", "E");
engine.put("list1", list1);
String jsCode = "var index; var values =list1.toArray();"
+ "println('Java to Javascript');for(index in values) {"
+ " println(values[index]);}";
engine.eval(jsCode);
jsCode = "importPackage(java.util);var list2 = Arrays.asList(['A', 'B', 'C']); ";
engine.eval(jsCode);
List<String> list2 = (List<String>) engine.get("list2");
for (String val : list2) {
System.out.println(val);
}
}
}
Related examples in the same category