I need to access an internal class from nashorn inside a web application running in WildFly.
The following code is working fine in my machine:
public class NashornTest {
public static void main(String[] args) throws ClassNotFoundException {
Class<?> cls = Class.forName("jdk.nashorn.internal.objects.ScriptFunctionImpl");
System.out.println(cls);
}
}
But the following servlet is throwing a ClassNotFoundException when I run inside WildFly:
@WebServlet("/nashorn")
public class NashornServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Class<?> cls = null;
try {
cls = Class.forName("jdk.nashorn.internal.objects.ScriptFunctionImpl");
} catch (ClassNotFoundException e) {
throw new ServletException(e);
}
}
}
Here's the server log: https://gist.github.com/csokol/b81633bdbfa09bb55e2b
Looks like there was an oversight in what all packages / classes nashorn uses.
You will need to open WILDFLY_HOME/modules/system/layers/base/sun/jdk/main/module.xml file and add
<path name="jdk/nashorn/api/scripting"/>
<path name="jdk/nashorn/api/scripting/resources"/>
<path name="jdk/nashorn/internal/codegen"/>
<path name="jdk/nashorn/internal/codegen/types"/>
<path name="jdk/nashorn/internal/ir"/>
<path name="jdk/nashorn/internal/ir/annotations"/>
<path name="jdk/nashorn/internal/ir/debug"/>
<path name="jdk/nashorn/internal/ir/visitor"/>
<path name="jdk/nashorn/internal/lookup"/>
<path name="jdk/nashorn/internal/objects"/>
<path name="jdk/nashorn/internal/objects/annotations"/>
<path name="jdk/nashorn/internal/parser"/>
<path name="jdk/nashorn/internal/runtime"/>
<path name="jdk/nashorn/internal/runtime/arrays"/>
<path name="jdk/nashorn/internal/runtime/linker"/>
<path name="jdk/nashorn/internal/runtime/options"/>
<path name="jdk/nashorn/internal/runtime/regexp"/>
<path name="jdk/nashorn/internal/runtime/regexp/joni"/>
<path name="jdk/nashorn/internal/runtime/resources"/>
<path name="jdk/nashorn/internal/runtime/resources/fx"/>
<path name="jdk/nashorn/internal/runtime/scripts"/>
<path name="jdk/nashorn/internal/tools"/>
<path name="jdk/nashorn/internal/tools/resources"/>
<path name="jdk/internal/dynalink"/>
<path name="jdk/internal/dynalink/beans"/>
<path name="jdk/internal/dynalink/linker"/>
<path name="jdk/internal/dynalink/support"/>
and any other path/package that might be missing.
for reference, this is how this file looks in 8.x branch
I also created Pull requests to fix this in WildFly master and 8.x branch
When running under security manager with the Java platform default security policy (or compatible extension of it), any class in jdk.nashorn.internal.* is "privileged" and hence not accessible to a caller unless specifically allowed to access (see package.access property $JDK/jre/lib/security/java.security file). Also, as you can see from the name, this class is internal. If you can elaborate of the use-case (i.e., why you need to access ScriptFunctionImpl), we may be able to suggest a different approach.