Java通过ScriptEngine 执行js脚本案例

public static void main(String[] args) throws ScriptException, FileNotFoundException, NoSuchMethodException {
        ScriptEngine engine = new ScriptEngineManager().getEngineByName("javascript");
        
        engine.eval(new FileReader("G:\\tmp\\test.js"));
        
        if(engine instanceof Invocable){
            Invocable in = (Invocable)engine;
            
            Double result = (Double)in.invokeFunction("add", 100.0,200.0);
            
            System.out.println("Result:"+result);
        }
    }

 

posted @ 2018-02-05 22:06  yshy  阅读(1978)  评论(0编辑  收藏  举报