日期:2014-05-16 浏览次数:20393 次
ScriptEngineManager manager = new ScriptEngineManager(); ScriptEngine engine = manager.getEngineByName("js"); System.out.println(engine.getClass()); engine.put("age", 21); engine.eval("if (age >= 18){ " + " print('Old enough to vote!'); " + "} else {" + " print ('Back to school!');" + "}");
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 = engine.eval("c = a + b;"); System.out.println("a + b = " + result);