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);
	}
}
相关推荐
小蒜学长5 分钟前
vue家教预约平台设计与实现(代码+数据库+LW)
java·数据库·vue.js·spring boot·后端
串串狗xk8 分钟前
使用 webgl 写的新概念笔记应用《赛博城寨》,在三维开放世界里写笔记
javascript·webgl
跟橙姐学代码18 分钟前
手把手教你玩转 multiprocessing,让程序跑得飞起
前端·python·ipython
天天摸鱼的java工程师18 分钟前
谈谈你对 Seata 的理解?8 年 Java 开发:从业务踩坑到源码级解析(附实战代码)
java·后端·面试
Emrys_21 分钟前
基于 AOP 实现接口幂等性 —— 深入浅出实战指南
java
用户37215742613522 分钟前
Java PPT转多种图片格式:打造高质量的文档转换服务
java
LSTM9722 分钟前
如何使用Java将PDF转换为Word
java
华仔啊22 分钟前
SpringBoot+MySQL+Vue实现文件共享系统
java·前端·后端
long31635 分钟前
代理设计模式
java·学习·程序人生·设计模式·代理模式
LCS-31238 分钟前
Python爬虫实战: 爬虫常用到的技术及方案详解
开发语言·爬虫·python