ScriptEngineManager类(Java和Js互相调⽤)JDK8⼦⽈:“道千乘之国,敬事⽽信,节⽤⽽爱⼈,使民以时。”
Java和Js之间的调⽤时基于ScriptEngineManager类,这个类是jdk6就有的 (谢谢@李患者 的提醒)
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javax.script.Bindings;
import javax.script.Invocable;
import javax.script.ScriptContext;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.SimpleScriptContext;
import org.junit.Test;
public class JavaScriptFunction {
@Test
public void print() throws Exception{
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = EngineByName("JavaScript");
engine.eval("print('hello word!!')");
}
@Test
public void obj() throws Exception {
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = EngineByName("JavaScript");
StringBuffer script = new StringBuffer();
script.append("var obj = new Object();");
script.append("obj.hello = function(name){print('hello, '+name);}");
//执⾏这段script脚本
engine.String());
// javax.script.Invocable 是⼀个可选的接⼝
// 检查你的script engine 接⼝是否已实现!
// 注意:JavaScript engine实现了Invocable接⼝
Invocable inv = (Invocable) engine;
// 获取我们想调⽤那个⽅法所属的js对象
Object obj = ("obj");
// 执⾏obj对象的名为hello的⽅法
inv.invokeMethod(obj, "hello", "Script Method !!" );
}
@Test
public void file() throws Exception{
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = EngineByName("JavaScript");
engine.eval(new java.io.FileReader(new File("F:/test/test.js")));
Invocable inv = (Invocable) engine;
Object obj = ("obj");
inv.invokeMethod(obj, "name", "知道了" );
}
/**
* 脚本变量
* @throws Exception
*/
@Test
public void scriptVar() throws Exception{
public void scriptVar() throws Exception{
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = EngineByName("JavaScript");
File file = new File("F:/");
//将File对象f直接注⼊到js脚本中并可以作为全局变量使⽤
engine.put("files", file);
engine.eval("Path());Name());");
}
/**
*  使⽤Script实现java接⼝
* @throws Exception
*/
public void runnableImpl() throws Exception{
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = EngineByName("JavaScript");
// String⾥定义⼀段JavaScript代码脚本
String script = "function run() { print('run called'); }";
// 执⾏这个脚本
engine.eval(script);
// 从脚本引擎中获取Runnable接⼝对象(实例). 该接⼝⽅法由具有相匹配名称的脚本函数实现。        Invocable inv = (Invocable) engine;
// 在上⾯的脚本中,我们已经实现了Runnable接⼝的run()⽅法
Runnable runnable = Interface(Runnable.class);
// 启动⼀个线程运⾏上⾯的实现了runnable接⼝的script脚本
Thread thread = new Thread(runnable);
thread.start();
}
/**
* 脚本引擎的多个scope
* @throws Exception
*/
@Test
public void multiScopes() throws Exception{
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = EngineByName("nashorn");
// 打印全局变量 "x"
engine.put("x", "hello word!!");
engine.eval("print(x);");
// 上⾯的代码会打印"hello word!!"
// 现在,传⼊另⼀个不同的script context
js脚本编程入门ScriptContext context = new SimpleScriptContext();
//新的Script context绑定ScriptContext的ENGINE_SCOPE
Bindings bindings = Bindings(ScriptContext.ENGINE_SCOPE);
// 增加⼀个新变脸到新的范围 engineScope 中
bindings.put("x", "word hello!!");
// 执⾏同⼀个脚本 - 但这次传⼊⼀个不同的script context
engine.eval("print(x);", bindings);
engine.eval("print(x);");
}
public static void main(String[] args) throws Exception {
new JavaScriptFunction().runnableImpl();
List list = new ArrayList();
list.add("1");
list.add("1");
list.add("1");
for (Object object : list) {
System.out.println(object);
}
}
}
作者是⼀名⾃由程序员,住在上海,喜欢⾳乐、⼩说、旅⾏、以及编程。P.S. 如果您喜欢这篇⽂章并且希望学习编程技术的话,请关注⼀下

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。