编程语言
首页 > 编程语言> > java执行脚本文件

java执行脚本文件

作者:互联网

JDK8中新添加了ScriptEngineManager类用于调用脚本文件

ScriptEngineManager类常用方法

ScriptEngine接口常用方法

代码示例

public void hello() throws Exception{
        ScriptEngineManager manager = new ScriptEngineManager();
        //JavaScript可以简写为js
        ScriptEngine engine = manager.getEngineByName("JavaScript");
        //也可以采用ScriptEngine engine = new ScriptEngineManager().getEngineByExtension("js");
        engine.eval("print('hello word!!')");
    }

hello.js脚本文件

print(hello);//支持print()打印
public void hello() throws Exception {    
    ScriptEngine engine = new ScriptEngineManager().getEngineByName("js");    
    String script = FileCopyUtils.copyToString(new  FileReader("hello.js"));    
    String hello = "hello word!";    
    engine.put("hello", hello);   
    engine.eval(script);
    }

标签:脚本,文件,java,String,js,engine,ScriptEngine,hello,ScriptEngineManager
来源: https://www.cnblogs.com/lingdu9527/p/11857918.html