Java ScriptEngine pass in value from Java to Javascript
import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; import javax.script.ScriptException; import javax.script.SimpleBindings; public class Main { public static void main(String[] args) { String myJavaString = "This is a Java parameter!"; ScriptEngineManager sem = new ScriptEngineManager(); ScriptEngine nashorn = sem.getEngineByName("nashorn"); try {// w ww . java 2s. co m SimpleBindings simpleBindings = new SimpleBindings(); simpleBindings.put("myString", myJavaString); nashorn.eval("print (myString)", simpleBindings); SimpleBindings simpleBindings2 = new SimpleBindings(); simpleBindings2.put("v1", 2); simpleBindings2.put("v2", 3); nashorn.eval( "function add(a, b){var v = a +b; "// + "return v; }"// + "print(add(v1, v2));", simpleBindings2); } catch (ScriptException ex) { ex.printStackTrace(); } } }