Actually, the correct way to find out if the object returned by the script is undefined is a ScriptObjectMirror request:
import jdk.nashorn.api.scripting.ScriptObjectMirror; Object m = engine.get("out"); if (ScriptObjectMirror.isUndefined(m)) { System.out.println("m is undefined"); }
Alternative way using Nashorn's internal API
You can also do this by checking its type:
import jdk.nashorn.internal.runtime.Undefined; Object m = engine.get("out"); if (m instanceof Undefined) { System.out.println("m is undefined"); }
Note that Nashorn did not make part of the undefined type of the public API, so using this can be problematic (they can change this between releases), so use ScriptObjectMirror . Just added it here as a curiosity ...
source share