如何使用Javascript Eval修改Java程序的变量?

时间:2018-09-28 13:10:10

标签: javascript java

我想使用其内置的Javascript引擎修改Java程序中的变量。这样可以吗?

ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("JavaScript");

Integer x = new Integer(11);
Integer y = new Integer(20);

Bindings bindings = new SimpleBindings();       
bindings.put("x", x);
bindings.put("y", y);

engine.eval("if (x > 10) {" + 
            "   print(\"Entered condition\");" +
            "   y = 90;" + 
            "}", bindings);

System.out.println(bindings.get("y"));

以上打印20。

0 个答案:

没有答案