早在Java 6时就引入了JavaScript引擎 Rhino
,它支持ECMAScript 5.1规范,它使用JSR 292言特性。Java 7时引入了 invokedynamic
,将JavaScript编译成 Java字节码
。Java 8引入的新的JavaScript引擎 Nashorn
比 Rhino
性能提高多倍, Nashorn
就是 javax.script.ScriptEngine
的另一种实现。
jjs
是个基于 Nashorn
引擎的命令行工具。它可以接受JavaScript源代码为参数,并且执行这些源代码。
为了使 Java
和 JavaScript
两门语言在 JVM
环境下能相互调用。
C:/Users/Administrator/Desktop/back/new λ touch test.js C:/Users/Administrator/Desktop/back/new λ vim test.js C:/Users/Administrator/Desktop/back/new λ cat test.js print("This is JavaScript test code."); C:/Users/Administrator/Desktop/back/new λ jjs test.js This is JavaScript test code. # 交互式编程 C:/Users/Administrator/Desktop/back/new λ jjs jjs> print("this is JavaScript test code") this is JavaScript test code jjs> # 传递参数 C:/Users/Administrator/Desktop/back/new λ jjs -- a b c jjs> print("传递的参数:"+arguments.join(",")) 传递的参数:a,b,c jjs>
import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; import javax.script.ScriptException; public class Test13 { public static void main(String[] args) { java_JavaScript(); } private static void java_JavaScript() { ScriptEngineManager scriptEngineManager = new ScriptEngineManager(); ScriptEngine nashorn = scriptEngineManager.getEngineByName("nashorn"); String name = "huangdayu.cn"; Integer result = null; try { nashorn.eval("print('" + name + "')"); result = (Integer) nashorn.eval("10 + 2"); } catch (ScriptException e) { System.out.println("执行脚本错误: " + e.getMessage()); } System.out.println(result.toString()); } }
huangdayu.cn 12
var BigDecimal = Java.type('java.math.BigDecimal'); function calculate(amount, percentage) { var result = new BigDecimal(amount).multiply( new BigDecimal(percentage)).divide(new BigDecimal("100"), 2, BigDecimal.ROUND_HALF_EVEN); return result.toPlainString(); } var result = calculate(568000000000000000023,13.9); print(result);
F:/Android/Java/Eclipse Project SSM/NewCharacteristic/js λ touch test2.js F:/Android/Java/Eclipse Project SSM/NewCharacteristic/js λ jjs test2.js 78952000000000002017.94
Java 8 新特性
Java 8 新特性终极指南