Looking at the Rhino to Nashorn porting guide , I see several possible ways.
If you are not using compatibility with the Rhino script, this would do this:
var usingNashorn = typeof importClass !== "function";
... since importClass is defined for Rhino, but not for Nashorn (unless you have enabled script compatibility).
I think Java.type is specific to Nashorn, therefore:
var usingNashorn = typeof Java !== "undefined" && Java && typeof Java.type === "function";
You can check for exceptions:
var usingNashorn; try { // Anything that will throw an NPE from the Java layer java.lang.System.loadLibrary(null); } catch (e) { // false! usingNashorn = e instanceof java.lang.NullPointerException; }
... since the migration guide says it will be true for Nashorn, but false for Rhino. This is because you selected an exception, which is unsuccessful.
source share