I am migrating old code from JDK 8 to JDK 12.
In the process, I have noticed importPackage
does not exist when using the "graal.js"
script engine. It exists when using "javascript"
for the script engine.
Is there any way to achieve the same functionality with "graal.js"
? The Nashorn migration doc on the GraalJS repository does not cover this.
importPackage
is originally from Rhino. Even Nashorn supports it when Rhino/Mozilla compatibility is requested explicitly using load("nashorn:mozilla_compat.js");
only, see Rhino Migration Guide in the documentation of Nashorn.
Graal.js has Nashorn compatibility mode and it supports load("nashorn:mozilla_compat.js");
in this mode.
So, you can use something like
System.setProperty("polyglot.js.nashorn-compat", "true");
ScriptEngine engine = new ScriptEngineManager().getEngineByName("graal.js");
System.out.println(engine.eval("load('nashorn:mozilla_compat.js'); importPackage('java.awt'); new Point();"));
(it prints java.awt.Point[x=0,y=0]
which shows that the package java.awt
was imported successfully).