Use an external file containing the javascript code
import java.io.InputStreamReader;
import java.util.List;
import javax.script.Invocable;
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");
engine.eval(new InputStreamReader
(Main.class.getResourceAsStream("s.js")));
List <String> list1 = (List <String>)engine.get("list1");
if (list1 != null) {
for (String s : (List<String>) list1) {
System.out.println(s);
}
}
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);
}
}
}
}
}
Related examples in the same category