admin管理员组文章数量:1794759
Java 中使用 JavaScript Nashorn 引擎
Nashorn 介绍
Nashorn 是 Java 8 中引入的 JavaScript 引擎,它允许在 Java 应用程序中嵌入和执行 JavaScript 代码。但是在JDK 15 中,Nashorn 已经被移除,取而代之的是新的 JavaScript 引擎,即 GraalJS。如果要继续使用 Nashorn,需要引入相应的依赖。
代码语言:javascript代码运行次数:0运行复制<dependency>
<groupId>org.openjdk.nashorn</groupId>
<artifactId>nashorn-core</artifactId>
<version>15.0</version>
</dependency>
hello world
准备javascript文件,内容如下:
代码语言:javascript代码运行次数:0运行复制var greeting='hello world';
print(greeting);
在Java 中使用 Nashorn 引擎执行这个文件:
代码语言:javascript代码运行次数:0运行复制import org.apachemons.io.FileUtils;
import javax.script.*;
import java.io.File;
public class TestNashorn {
public static void main( String args[] ) throws Exception {
ScriptEngine engine = new ScriptEngineManager().getEngineByName("Nashorn");
String content = FileUtils.readFileToString(new File(TestNashorn.class.getResource("test.js").getFile()), "UTF-8");
Object result = engine.eval(content);
System.out.println("result: " + result);
}
}
传递参数
准备javascript文件,内容如下:
代码语言:javascript代码运行次数:0运行复制var greeting='hello ' + name;
print(greeting);
greeting
javascript 中使用了一个变量name,最后返回了greeting。
在Java 中使用 Nashorn 引擎执行这个文件,其中使用 Bindings 传递了name参数,然后获取返回值:
代码语言:javascript代码运行次数:0运行复制import org.apachemons.io.FileUtils;
import javax.script.*;
import java.io.File;
public class TestNashorn {
public static void main( String args[] ) throws Exception {
ScriptEngine engine = new ScriptEngineManager().getEngineByName("Nashorn");
Bindings bind = engine.getBindings(ScriptContext.ENGINE_SCOPE);
bind.put("name", "kongxx");
String content = FileUtils.readFileToString(new File(TestNashorn.class.getResource("greeting.js").getFile()), "UTF-8");
Object result = engine.eval(content);
System.out.println("result: " + result);
}
}
调用 Java 类和方法
准备javascript文件,其中调用了 Java 中的 Calendar 类,内容如下:
代码语言:javascript代码运行次数:0运行复制load('nashorn:mozilla_compat.js');
//importPackage(Packages.java.util);
importClass(java.util.Calendar);
var cal = Calendar.getInstance();
var time = cal.getTimeInMillis();
print(time);
time
在Java 中使用 Nashorn 引擎执行这个文件,然后获取返回值:
代码语言:javascript代码运行次数:0运行复制import org.apachemons.io.FileUtils;
import javax.script.*;
import java.io.File;
public class TestNashorn {
public static void main( String args[] ) throws Exception {
ScriptEngine engine = new ScriptEngineManager().getEngineByName("Nashorn");
Bindings bind = engine.getBindings(ScriptContext.ENGINE_SCOPE);
String content = FileUtils.readFileToString(new File(TestNashorn.class.getResource("javaclass.js").getFile()), "UTF-8");
Object result = engine.eval(content);
System.out.println("result: " + result);
}
}
调用 JavaScript 函数
准备javascript文件,其中定义了两个函数,内容如下:
代码语言:javascript代码运行次数:0运行复制var func1 = function() {
return 'hello world';
}
var func2 = function(name) {
return 'hello '+ name;
}
在Java 中使用 Nashorn 引擎执行这个文件,其中使用 Invocable 调用函数,并获取返回值:
代码语言:javascript代码运行次数:0运行复制import org.apachemons.io.FileUtils;
import javax.script.*;
import java.io.File;
public class TestNashorn {
public static void main( String args[] ) throws Exception {
ScriptEngine engine = new ScriptEngineManager().getEngineByName("Nashorn");
String content = FileUtils.readFileToString(new File(TestNashorn.class.getResource("function.js").getFile()), "UTF-8");
engine.eval(content);
Invocable invocable = (Invocable)engine;
Object result1 = invocable.invokeFunction("func1");
System.out.println("result: " + result1);
Object result2 = invocable.invokeFunction("func2", "kongxx");
System.out.println("result: " + result2);
}
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。 原始发表:2024-10-09,如有侵权请联系 cloudcommunity@tencent 删除变量函数javajavascriptimport本文标签: Java 中使用 JavaScript Nashorn 引擎
版权声明:本文标题:Java 中使用 JavaScript Nashorn 引擎 内容由林淑君副主任自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.xiehuijuan.com/baike/1754933396a1708333.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论