java调js基础
public static void main(String[] args)throws Exception { ScriptEngine se = new ScriptEngineManager().getEngineByName("javascript"); Bindings p = se.createBindings(); p.put("factor", 3); se.setBindings(p, ScriptContext.ENGINE_SCOPE); Scanner sc = new Scanner(System.in); while (sc.hasNextInt()) { int each = sc.nextInt(); int each2 =sc.nextInt(); se.eval(new FileReader(new File("D:/_TMP/_tmp.js"))); if (se instanceof Invocable) { Invocable invocable = (Invocable)se; double result = (double)invocable.invokeFunction("calc",each,each2); System.out.println("执行js:"+result); } } }
function calc(each,each2){ return each + each2 * factor; }