Java编程Nashorn实例代码
作者:CrazeBean 时间:2022-07-30 05:28:21
本文研究的主要是Java编程Nashorn的相关内容,具体如下。
Nashorn是什么
Nashorn,发音“nass-horn”,是德国二战时一个坦克的命名,同时也是java8新一代的javascript引擎--替代老旧,缓慢的Rhino,符合 ECMAScript-262 5.1 版语言规范。你可能想javascript是运行在web浏览器,提供对html各种dom操作,但是Nashorn不支持浏览器DOM的对象。这个需要注意的一个点。
之前学习Java8的时候恰好写了个简单的例子,所以放在这里记录一下。
文件目录:
StringFunction.java,字符串功能类
StringNashorn.java,封装脚本引擎
NashornTest.java,测试引擎,引擎调用
StringFunction.java源码:
public class StringFunction {
/**
* 字符串截取
*/
public String sub(String str, int start, int end) {
return str.substring(start, end);
}
/**
* 字符串拼接
*/
public String append(String... strs) {
StringBuilder result = new StringBuilder(strs[0]);
Stream.of(strs).skip(1).forEach(str -> result.append(str));
return result.toString();
}
}
StringNashorn.java源码:
public class StringNashorn {
/**
* Nashorn脚本引擎
*/
private ScriptEngine nashorn = new ScriptEngineManager().getEngineByName("nashorn");
/**
* 执行脚本
*/
public Object execute(String script) {
ScriptContext scriptContext = new SimpleScriptContext();
// 定义一个名为stringfunction的函数,这个函数实际对应着一个StringFunction对象
scriptContext.setAttribute("stringfunction", new StringFunction(), 100);
nashorn.setContext(scriptContext);
Object result = null;
try {
result = nashorn.eval(script);
} catch (ScriptException e) {
e.printStackTrace();
}
return result;
}
}
NashornTest.java源码:
public class NashornTest {
public static void main(String[] args) {
String substring = "stringfunction.sub(\"abcdefghijk\", 1, 4);";
String append = "stringfunction.append(\"abc\", \"def\");";
StringNashorn nashorn = new StringNashorn();
Object subResult = nashorn.execute(substring);
Object appendResult = nashorn.execute(append);
System.out.println(subResult.toString());
System.out.println(appendResult.toString());
}
}
运行main方法,运行结果:
bcd
abcdef
这里如果NashornTest.java改写如下:
public class NashornTest {
public static void main(String[] args) {
// 脚本内用对象接收结果并打印
String substring = "var s1 = stringfunction.sub(\"abcdefghijk\", 1, 4);"
+ " print(s1);";
String append = "var s2 = stringfunction.append(\"abc\", \"def\");"
+ " print(s2);";
StringNashorn nashorn = new StringNashorn();
// 这里execute不再返回对象,因为在脚本里面已经有对象接收sub和append的执行结果。
nashorn.execute(substring);
nashorn.execute(append);
}
}
同样也会输出相同的结果。
来源:http://blog.csdn.net/u010429286/article/details/78951707
标签:java,nashorn,新特性
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
浅谈java中OO的概念和设计原则(必看)
2023-11-24 13:09:56
Spring不能注入Static变量的原因及Spring注入静态变量
2023-11-24 04:16:36
解决try-catch捕获异常信息后Spring事务失效的问题
2022-11-15 03:17:33
Spring Boot 2.7.6整合redis与低版本的区别
2023-11-11 19:44:56
![](https://img.aspxhome.com/file/2023/7/59697_0s.png)
C#词法分析器之正则表达式的使用
2023-06-21 13:10:58
![](https://img.aspxhome.com/file/2023/2/66592_0s.png)
三道java新手入门面试题,通往自由的道路--多线程
2023-05-24 23:12:51
![](https://img.aspxhome.com/file/2023/9/61269_0s.jpg)
Springboot启动不检查JPA的数据源配置方式
2022-10-10 06:47:05
![](https://img.aspxhome.com/file/2023/4/68774_0s.jpg)
使用开源项目JAVAE2 进行视频格式转换
2023-11-08 07:18:24
![](https://img.aspxhome.com/file/2023/6/58846_0s.png)
Java四种权限修饰符知识点详解
2023-11-11 06:12:59
web 容器的设计如何实现
2022-04-07 00:29:51
![](https://img.aspxhome.com/file/2023/2/68772_0s.jpg)
详解Java内存泄露的示例代码
2023-06-08 03:34:51
Java对象不使用时赋值null的意义详解
2023-11-25 01:46:20
![](https://img.aspxhome.com/file/2023/4/59884_0s.png)
浅谈mybatis中SQL语句给boolean类型赋值问题
2023-01-19 15:15:42
java并发高的情况下用ThreadLocalRandom来生成随机数
2022-10-30 12:42:03
![](https://img.aspxhome.com/file/2023/6/69076_0s.jpg)
Lombok中@EqualsAndHashCode注解的使用及说明
2023-11-30 04:47:05
![](https://img.aspxhome.com/file/2023/4/64204_0s.png)
Flutter Widgets之标签类控件Chip详解
2023-06-26 14:22:35
![](https://img.aspxhome.com/file/2023/0/120090_0s.jpg)
spring mvc实现登录账号单浏览器登录
2022-06-28 22:29:47
Mybatis常用分页插件实现快速分页处理技巧
2022-12-25 00:03:45
.net 随机生成汉字
2022-01-22 08:33:33
SpringBoot+Hutool+thymeleaf完成导出Excel的实现方法
2023-09-05 17:39:10
![](https://img.aspxhome.com/file/2023/2/71682_0s.png)