Search code examples
javawildflynashornwildfly-8

Nashorn class not found in WildFly


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


Solution

  • 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