import javax.script.Bindings;
import javax.script.ScriptContext;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
public class BindingDemo {
public static void main(String[] args) {
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("js");
engine.put("a", 1);
engine.put("b", 5);
Bindings bindings = engine.getBindings(ScriptContext.ENGINE_SCOPE);
Object a = bindings.get("a");
Object b = bindings.get("b");
System.out.println("a = " + a);
System.out.println("b = " + b);
Object result;
try {
result = engine.eval("c = aaaa + bbbb;");
System.out.println("a + b = " + result);
} catch (ScriptException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
a = 1
b = 5
javax.script.ScriptException: sun.org.mozilla.javascript.internal.EcmaError: ReferenceError: "aaaa" is not defined. (#1) in at line number 1
at com.sun.script.javascript.RhinoScriptEngine.eval(RhinoScriptEngine.java:110)
at com.sun.script.javascript.RhinoScriptEngine.eval(RhinoScriptEngine.java:124)
at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:247)
at BindingDemo.main(BindingDemo.java:22)