Java 中使用 JavaScript Nashorn 引擎

Nashorn 介绍

Nashorn 是 Java 8 中引入的 JavaScript 引擎,它允许在 Java 应用程序中嵌入和执行 JavaScript 代码。但是在JDK 15 中,Nashorn 已经被移除,取而代之的是新的 JavaScript 引擎,即 GraalVM。如果要继续使用 Nashorn,需要引入相应的依赖。

xml 复制代码
<dependency>
  <groupId>org.openjdk.nashorn</groupId>
  <artifactId>nashorn-core</artifactId>
  <version>15.0</version>
</dependency>

hello world

准备javascript文件,内容如下:

javascript 复制代码
var greeting='hello world';
print(greeting);

在Java 中使用 Nashorn 引擎执行这个文件:

java 复制代码
import org.apache.commons.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 复制代码
var greeting='hello ' + name;
print(greeting);
greeting

javascript 中使用了一个变量name,最后返回了greeting。

在Java 中使用 Nashorn 引擎执行这个文件,其中使用 Bindings 传递了name参数,然后获取返回值:

java 复制代码
import org.apache.commons.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 复制代码
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 引擎执行这个文件,然后获取返回值:

java 复制代码
import org.apache.commons.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 复制代码
var func1 = function() {
    return 'hello world';
}

var func2 = function(name) {
    return 'hello '+ name;
}

在Java 中使用 Nashorn 引擎执行这个文件,其中使用 Invocable 调用函数,并获取返回值:

java 复制代码
import org.apache.commons.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);
	}
}
相关推荐
apocelipes30 分钟前
golang unique包和字符串内部化
java·python·性能优化·golang
Geoking.1 小时前
NumPy zeros() 函数详解
python·numpy
Full Stack Developme1 小时前
java.text 包详解
java·开发语言·python
_院长大人_2 小时前
el-table-column show-overflow-tooltip 只能显示纯文本,无法渲染 <p> 标签
前端·javascript·vue.js
SevgiliD2 小时前
el-table中控制单列内容多行超出省略及tooltip
javascript·vue.js·elementui
刘梦凡呀2 小时前
C#获取钉钉平台考勤记录
java·c#·钉钉
丁浩6662 小时前
Python机器学习---2.算法:逻辑回归
python·算法·机器学习
best_virtuoso2 小时前
PostgreSQL 常见数组操作函数语法、功能
java·数据结构·postgresql
yudiandian20142 小时前
02 Oracle JDK 下载及配置(解压缩版)
java·开发语言
要加油哦~2 小时前
JS | 知识点总结 - 原型链
开发语言·javascript·原型模式