版本:delight-nashorn-sandbox-0.2.0
流程:
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 logs
JDK_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.NashornScriptEngineFactory
public 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
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,目前还在维护更新中;
参考:
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