java8 新特性 v8 nashorn 引擎(五)
簡單的javascript 作為文字輸出
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("nashorn");
String js;
js = "var map = Array.prototype.map \n";
js += "var names = [\"john\", \"jerry\", \"bob\"]\n";
js += "var a = map.call(names, function(name) { return name.length() })\n";
js += "print(a)";
try {
engine.eval(js);
} catch (ScriptException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ScriptEngine jse = new ScriptEngineManager().getEngineByName("nashorn");
try {
jse.eval("print('Hello DoubleCloud! - inline');");
} catch (ScriptException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
執行javascript 檔案
ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
ScriptEngine jse = scriptEngineManager.getEngineByName("nashorn");
System.out.println("current dir: " + new File(".").getAbsoluteFile());
try {
jse.eval(new FileReader("./src/com/test/zcw/java/script.js"));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (ScriptException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
js檔案語句:eval("print('Hello DoubleCloud! - inline');")
呼叫javascript 函式
ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
ScriptEngine jse = scriptEngineManager.getEngineByName("nashorn");
jse.eval(new FileReader("./src/com/test/zcw/java/script.js"));
Invocable runner = (Invocable) jse;
Object result = runner.invokeFunction("hi", "DoubleCloud! - via javascript function");
System.out.println(result);
System.out.println(result.getClass());
函式內容:
var hi = function(name)
{
return 'Hi ' + name;
}
javascript呼叫java
var hw = Java.type('com.test.zcw.java8.JSNashorn');
var result = hw.sayHi("DoubleCloud - calling Java method in JS.");
print(result);
java
static public String sayHi(String name) {
return "Hi " + name;
}
ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
ScriptEngine jse = scriptEngineManager.getEngineByName("nashorn");
jse.eval(new FileReader("./src/com/test/zcw/java/java8/script.js"));
相關文章
- JAVA8新特性Java
- Java8 新特性Java
- Java8新特性 - LambdaJava
- Java8的新特性Java
- JAVA8新特性用法Java
- Java8新特性之:OptionalJava
- Java8新特性--Stream APIJavaAPI
- java8新特性stream流Java
- Java8新特性實踐Java
- Java8新特性系列-LambdaJava
- Java8新特性系列(Stream)Java
- Java8新特性系列(Interface)Java
- Java8新特性系列(Lambda)Java
- Java8 新特性詳解Java
- java8新特性學習Java
- ?Java8新特性之Optional類Java
- java8 新特性之方法引用Java
- java8 新特性之Optional 類Java
- Java8 新特性之 Optional 類Java
- Java8的八個新特性Java
- Oracle11新特性——PLSQL新特性(五)OracleSQL
- Java8新特性(1):Lambda表示式Java
- Java8常用的新特性總結Java
- Java8新特性(一)-Lambda表示式Java
- java8 新特性之Lambda 表示式Java
- java8 新特性之預設方法Java
- Java8新特性探索之Stream介面Java
- Java8新特性系列(原子性操作)Java
- Java8 新特性(一)- 介面增強Java
- Java8 新特性之 Lambda 表示式Java
- Java8新特性都到碗裡來Java
- 【java8新特性】蘭姆達表示式Java
- JAVA8 新特性(二)轉載整理Java
- Java8新特性之時間APIJavaAPI
- Java 8 的 Nashorn 指令碼引擎教程Java指令碼
- JS引擎(2):Java平臺上JavaScript引擎—Rhino/Nashorn概述JSJavaScript
- Java8的新特性--函式式介面Java函式
- Java8新特性之日期-時間APIJavaAPI