编程语言
首页 > 编程语言> > Java执行JavaScript脚本

Java执行JavaScript脚本

作者:互联网

demo

//注入函数
StringBuilder scriptContent = new StringBuilder();

scriptContent.append("function run(){");
scriptContent.append("return 123");
scriptContent.append("}");
ScriptEngineManager factory = new ScriptEngineManager();
ScriptEngine engine = factory.getEngineByName("js");

engine.eval(scriptContent.toString());
Invocable inv = (Invocable)engine;
Object result = inv.invokeFunction("run");
System.out.println(result);

标签:脚本,engine,Java,inv,JavaScript,scriptContent,result,run,append
来源: https://www.cnblogs.com/zhuxiang1633/p/16471036.html