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);
原创文章,作者:kirin,如若转载,请注明出处:https://blog.ytso.com/273851.html