0 Replies Latest reply on Jul 10, 2015 5:33 AM by Cédric Tabin

    [EAP6.4.0] Nashorn advanced integration: classloader issue

    Cédric Tabin Newbie

      Hello,

       

      I'm trying to integrate nashorn in my Enterprise Application, but I'm facing some weird classloading issues.

      Here is the code I use in my EJB:

       

      ScriptEngineManager sem = new ScriptEngineManager();
      ScriptEngine scriptEngine = sem.getEngineByName("nashorn");
      
      //creation of an ObjectFactory instance
      scriptEngine.eval("function ObjectFactory() {}\n" +
                             "ObjectFactory.prototype.constructor = ObjectFactory;\n" +
                             "ObjectFactory.prototype.create = function() {return \"hello\";};\n"+;
                        "var objectFactory = new ObjectFactory();");
      
      Object obj = scriptEngine.get("objectFactory");
      Invocable invokable = (Invocable)scriptEngine;
      InternalObjectFactory iof = invokable.getInterface(obj, ObjectFactory.class); //boom !
      
      

       

      The interface ObjectFactory is very basic:

      public interface InternalObjectFactory {
         public Object create();
      }
      
      

       

      When the code is ran, I get the following exception:

      ECMAScript Exception: TypeError: Can not find a common class loader for ScriptObject and mypackage.javascript.ObjectFactory.
          at jdk.nashorn.internal.runtime.ECMAErrors.error(ECMAErrors.java:57)
          at jdk.nashorn.internal.runtime.ECMAErrors.typeError(ECMAErrors.java:213)
          at jdk.nashorn.internal.runtime.ECMAErrors.typeError(ECMAErrors.java:185)
          at jdk.nashorn.internal.runtime.ECMAErrors.typeError(ECMAErrors.java:172)
          at jdk.nashorn.internal.runtime.linker.AdaptationResult.typeError(AdaptationResult.java:73)
          at jdk.nashorn.internal.runtime.linker.JavaAdapterFactory$AdapterInfo.getAdapterClass(JavaAdapterFactory.java:283)
          at jdk.nashorn.internal.runtime.linker.JavaAdapterFactory.getAdapterClassFor(JavaAdapterFactory.java:131)
          at jdk.nashorn.internal.runtime.linker.JavaAdapterFactory.getAdapterClassFor(JavaAdapterFactory.java:117)
          at jdk.nashorn.internal.runtime.linker.JavaAdapterFactory.getConstructor(JavaAdapterFactory.java:163)
          at jdk.nashorn.api.scripting.NashornScriptEngine.getInterfaceInner(NashornScriptEngine.java:276)
          at jdk.nashorn.api.scripting.NashornScriptEngine.getInterface(NashornScriptEngine.java:209)
          ...
      
      

       

      It seems that nashorn is unable to find any of the class in my EAR. Even if I try to call Java.type in a script, I get the same exception.

      I tried to pass the current ClassLoader in the ScriptEngineManager, but nothing changed. Note also that this code runs correctly in Glassfish 4.1.

      Is there something special to configure in my application.xml or elsewhere ?

       

      Thanks in advance for any information