版本:delight-nashorn-sandbox-0.2.0

1. delight-nashorn-sandbox

流程:

  • 优先使用jdk中的nashorn
  • 其次使用openjdk中的nashorn
    static {JDK_NASHORN_ScriptObjectMirror_CLASS = findClass("jdk.nashorn.api.scripting.ScriptObjectMirror", "JDK-provided Nashorn");if (JDK_NASHORN_ScriptObjectMirror_CLASS != null) {JDK_NASHORN_NashornScriptEngineFactory_CLASS = findClass("jdk.nashorn.api.scripting.NashornScriptEngineFactory", "JDK-provided Nashorn");JDK_NASHORN_ClassFilter_CLASS = findClass("jdk.nashorn.api.scripting.ClassFilter", "JDK-provided Nashorn");} else {// no need to search for those and add more logsJDK_NASHORN_NashornScriptEngineFactory_CLASS = null;JDK_NASHORN_ClassFilter_CLASS = null;}STANDALONE_NASHORN_ScriptObjectMirror_CLASS = findClass("org.openjdk.nashorn.api.scripting.ScriptObjectMirror", "Standalone Nashorn");if (STANDALONE_NASHORN_ScriptObjectMirror_CLASS != null) {STANDALONE_NASHORN_NashornScriptEngineFactory_CLASS = findClass("org.openjdk.nashorn.api.scripting.NashornScriptEngineFactory", "Standalone Nashorn");STANDALONE_NASHORN_ClassFilter_CLASS = findClass("org.openjdk.nashorn.api.scripting.ClassFilter", "Standalone Nashorn");} else {STANDALONE_NASHORN_NashornScriptEngineFactory_CLASS = null;STANDALONE_NASHORN_ClassFilter_CLASS = null;}if (JDK_NASHORN_ScriptObjectMirror_CLASS == null && STANDALONE_NASHORN_ScriptObjectMirror_CLASS == null) {throw new RuntimeException("Neither JDK nor Standalone Nashorn was found. If running on JDK 15 or later, you must add standalone Nashorn to the classpath.");}logger.info("Delight-nashorn-sandbox detected that JDK Nashorn is {} and standalone Nashorn is {}, and will use {}",JDK_NASHORN_ScriptObjectMirror_CLASS != null ? "present" : "absent",STANDALONE_NASHORN_ScriptObjectMirror_CLASS != null ? "present" : "absent",JDK_NASHORN_ScriptObjectMirror_CLASS != null ? "JDK Nashorn" : "Standalone Nashorn");}// actually returns an instance of either jdk.nashorn.api.scripting.NashornScriptEngineFactory or org.openjdk.nashorn.api.scripting.NashornScriptEngineFactorypublic static Object getNashornScriptEngineFactory() {try {if (JDK_NASHORN_NashornScriptEngineFactory_CLASS != null) {return JDK_NASHORN_NashornScriptEngineFactory_CLASS.getConstructor().newInstance();} else if (STANDALONE_NASHORN_NashornScriptEngineFactory_CLASS != null) {return STANDALONE_NASHORN_NashornScriptEngineFactory_CLASS.getConstructor().newInstance();}throw new IllegalStateException("Neither jdk.nashorn.api.scripting.NashornScriptEngineFactory or org.openjdk.nashorn.api.scripting.NashornScriptEngineFactory is present");} catch (InstantiationException | InvocationTargetException | NoSuchMethodException | IllegalAccessException e) {throw new RuntimeException(e);}}

pom文件中的依赖

<dependency>
            <groupId>org.openjdk.nashorn</groupId>
            <artifactId>nashorn-core</artifactId>
            <version>15.2</version>
            <optional>true</optional>       
        </dependency>

optional为true,不传递依赖,所以,默认情况下,只引用delight-nashorn-sandbox的工程,运行时,只能引用到jdk nashorn:
Delight-nashorn-sandbox detected that JDK Nashorn is present and standalone Nashorn is absent, and will use JDK Nashorn

如果想引入openjdk nashorn,需要在工程pom文件中,再单独引入如上org.openjdk.nashorn:nashorn-core依赖即可;
注意: 本地使用jdk 8时,引入org.openjdk.nashorn:nashorn-core:15.2时,会报错,本地的jdk版本8,低于openjdk nashorn jar文件编译时的jdk版本11:
(class file version 55.0,对应Java 11)
Invocation of init method failed; nested exception is java.lang.UnsupportedClassVersionError: org/openjdk/nashorn/api/scripting/NashornScriptEngineFactory has been compiled by a more recent version of the Java Runtime (class file version 55.0), this version of the Java Runtime only recognizes class file versions up to 52.0

2. openjdk nashorn

When browsing these guides, mentally substitute org.openjdk.nashorn in place of jdk.scripting.nashorn module name and jdk.nashorn package name.
openjdk的包: org.openjdk.nashorn
替换jdk中的: jdk.scripting.nashorn    module
                     jdk.nashorn                  package

While standalone Nashorn is primarily meant to be used with Java 15 and later versions, it can also be used with Java versions 11 to 14 that have a built-in version of Nashorn too. See this page for details on use when both versions are present.
后续jdk nashorn废弃;

使用Java 11-14时,jdk nashorn和openjdk nashorn可以共存;    

使用Java 15及以后的jdk时,可以使用独立的openjdk nashorn,目前还在维护更新中;

3. jdk nashorn

参考:
https://github.com/javadelight/delight-nashorn-sandbox
https://github.com/openjdk/nashorn
https://blogs.oracle.com/nashorn/
https://github.com/szegedi/nashorn/wiki/Using-Nashorn-with-different-Java-versions   在不同Java版本下使用nashorn
http://mail.openjdk.java.net/pipermail/announce/2012-November/000139.html            发起nashorn project项目
http://openjdk.java.net/jeps/335                                                     废弃nashorn提议,跟不上js变化
http://openjdk.java.net/projects/nashorn/
https://www.oschina.net/news/96868/nashorn-may-be-deprecate

delight-nashorn-sanbox NashornDetection 兼容jdk nashorn和openjdk nashorn相关推荐

  1. nashorn 和 delight-nashorn-sandbox 学习笔记

    一.java的脚本执行引擎ScriptEngine 从JDK6开始,java就嵌入了对脚本的支持,这里的脚本指的是但非局限于JS这样的非java语言,当时使用的脚本执行引擎是基于Mozilla 的Rh ...

  2. Nashorn——在JDK 8中融合Java与JavaScript之力--转

    原文地址:http://www.infoq.com/cn/articles/nashorn 从JDK 6开始,Java就已经捆绑了JavaScript引擎,该引擎基于Mozilla的Rhino.该特性 ...

  3. java7 rhino,Java 7 可运行的 Nashorn,代替 Rhino

    惊现有人把 OpenJDK 上的 Nashorn dump 下来,使得 Java 7 都可以使用!源码在 https://bitbucket.org/ramonza/nashorn-backport/ ...

  4. Nashorn:JVM上的下一代javascript引擎

    目录 译者语 介绍 单独使用JavaScript Heredocs Shell invocations 其他好东西 内嵌Nashorn mustachejs Nashorn中使用Java 处理数组 I ...

  5. nashorn 性能_Oracle Nashorn:用于JVM的下一代JavaScript引擎

    nashorn 性能 在Oracle应用程序中将Oracle Nashorn用作命令行工具和嵌入式解释器的方案 在Java SE 7之前,JDK附带了基于Mozilla RhinoJavaScript ...

  6. nashorn 性能_Nashorn简介

    nashorn 性能 引入了Java 8和名为" Nashorn"的新JavaScript引擎. Nashorn基于Da Vinci Machine,该项目旨在为JVM添加动态语言 ...

  7. Java8新特性总结 -8.Nashorn , JavaScript引擎

    所有示例代码打包下载 : 点击打开链接 Java8新特性 : 接口新增默认方法和静态方法 Optional类 Lambda表达式 方法引用 Stream API - 函数式操作流元素集合 Date/T ...

  8. Java 8 新特性-菜鸟教程 (7) -Java 8 Nashorn JavaScript

    Java 8 Nashorn JavaScript Nashorn 一个 javascript 引擎. 从JDK 1.8开始,Nashorn取代Rhino(JDK 1.6, JDK1.7)成为Java ...

  9. java入门---Java 8 新特性之Nashorn JavaScript引擎

        Nashorn 一个 javascript 引擎.从JDK 1.8开始,Nashorn取代Rhino(JDK 1.6, JDK1.7)成为Java的嵌入式JavaScript引擎.Nashor ...

最新文章

  1. spring中的BeanPostProcessor
  2. python调用通达信公式_对照通达信一些指标的Python实现
  3. golang的GUI库,使用go-fyne设计一个代办事项APP
  4. 使用Volley StringRequest Get的方式进行发票查询操作
  5. 文字排版 - bootStrap4常用CSS笔记
  6. mysql1033错误存储引擎_MySQL Innodb存储引擎因为缓存配置出现的错误
  7. 排序分发“魔法照片”(洛谷P1583题题解,Java语言描述)
  8. 最新pvz服务器补偿码,阴阳师:补偿来了!大量活动导致服务器崩溃,现已修复且下发补偿...
  9. linux网络安装mysql_linux系统安装mysql
  10. mongo 查询显示字段_MongoDB查询操作限制返回字段的方法
  11. html js注册表单代码,用户注册常用javascript代码
  12. React Native踩坑新建的RN0.64项目无法在xcode 12.5上打开
  13. 解决java.io.EOFException 异常
  14. VLC ass字幕文件格式
  15. 总结 工作法(时间管理+复盘)
  16. Simphony学习2 安全相关(密码和角色)
  17. 计算机硬盘格式化了如何恢复出厂设置,怎么把电脑格式化?
  18. 大数据和云计算有什么关系?
  19. 【ARM 嵌入式 C 入门及渐进 3 -- GCC __attribute__ 使用】
  20. 微信小程序 三角形实现 (评论三角形)

热门文章

  1. 服务器主板bios信息修改,服务器主板 bios设置
  2. [ABAP] QA32冲销BAPI示例代码
  3. 微信小程序 view样式
  4. 奥卡姆剃刀法则:用信息论解释简约之法则(Occam‘s Razor)【设法只保留少量的,等同于全部信息的有效信息。】
  5. leetcode292. Nim 游戏(java python3)
  6. 微服务架构—服务降级
  7. Centos 如何挂载磁盘
  8. VMware虚拟机网络设置及hostname设置
  9. JAVA基础-jdk和jre的关系和区别
  10. 进入SeekTiger创世节点,以开启你的元宇宙之旅