js方法如下:
function add(a,b){ return a + b + number; }
调用方法如下:(免费学习视频教程分享:java视频教程)
package com.cgnb.dataqualitymng; import java.io.FileReader; import java.util.Scanner; import javax.script.Bindings; import javax.script.Invocable; import javax.script.ScriptContext; import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; public class ExecuJs { public static void main(String[] args) throws Exception { // 获取JS执行引擎 ScriptEngine se = new ScriptEngineManager().getEngineByName("javascript"); // 获取变量 Bindings bindings = se.createBindings(); bindings.put("number", 3); se.setBindings(bindings, ScriptContext.ENGINE_SCOPE); Scanner sc = new Scanner(System.in); while (sc.hasNextInt()) { int a = sc.nextInt(); int b = sc.nextInt(); System.out.println("输入的参数【" + a + "】 + 【" + b + "】"); se.eval(new FileReader("D:\\desk\\test.js")); // 是否可调用 if (se instanceof Invocable) { Invocable in = (Invocable) se; Integer result = (Integer) in.invokeFunction("add", a, b); System.out.println("获得的结果:" + result); } } } }
调用结果如图:
相关文章教程推荐:java入门教程
以上是java如何调用js方法的详细内容。更多信息请关注PHP中文网其他相关文章!