Nashorn integration with third party libraries

Rémi Barraquand dev at remibarraquand.com
Fri Apr 18 08:24:53 UTC 2014


Hi all,

I might not be the first person to ask such question but I didn't found 
that much information regarding my issue on the official Nashorn page.

*Context*: In an open-source project I'm working on, I'm using JavaFx 
WebView to develop a client application written mostly in JavaScript 
(RequireJS, Backbone, JQuery, Handlebars, etc). The application is 
running perfectly. I nevertheless want to add a Command-Line Interface 
(CLI) to let users interact with the application (in my case access a 
compiler written in JS) from their terminal without having to open a 
windowed interface (GUI). I therefore want to execute some JS code 
without the use of the JavaFx WebView. As a result I'm trying to make 
use of Nashorn!

*Problem*: The code I'm trying to execute in the Nashorn script engine 
is using third party libraries such as Backbone Models and JQuery for 
the generation and manipulation of HTML (I do not need to render 
anything, just to generate HTML). I therefore tried to load JQuery 
library within Nashorn but I got a strange error...

The code:

|>  ScriptEngine engine  = manager.getEngineByName("nashorn");
>  engine.eval(new InputStreamReader(CLI.class.getResourceAsStream("/io/dahuapp/core/scripts/jquery.js")));
|

The error:

|--- ERROR ---
    Exception in thread "main" javax.script.ScriptException: TypeError: Cannot read property "defaultView" from undefined in<eval>  at line number 1010
at jdk.nashorn.api.scripting.NashornScriptEngine.throwAsScriptException(NashornScriptEngine.java:564)
at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:548)
at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:528)
at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:524)
at jdk.nashorn.api.scripting.NashornScriptEngine.eval(NashornScriptEngine.java:189)
at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:249)
at io.dahuapp.cli.CLI.main(CLI.java:45)
Caused by:<eval>:1010 TypeError: Cannot read property "defaultView" from undefined
     at jdk.nashorn.internal.runtime.ECMAErrors.error(ECMAErrors.java:56)
     at jdk.nashorn.internal.runtime.ECMAErrors.typeError(ECMAErrors.java:212)
     at jdk.nashorn.internal.runtime.ECMAErrors.typeError(ECMAErrors.java:184)
     at jdk.nashorn.internal.runtime.ECMAErrors.typeError(ECMAErrors.java:171)
     at jdk.nashorn.internal.runtime.Undefined.lookupTypeError(Undefined.java:128)
     at jdk.nashorn.internal.runtime.Undefined.lookup(Undefined.java:113)
     at jdk.nashorn.internal.runtime.linker.NashornLinker.getGuardedInvocation(NashornLinker.java:98)
     at jdk.internal.dynalink.support.CompositeTypeBasedGuardingDynamicLinker.getGuardedInvocation(CompositeTypeBasedGuardingDynamicLinker.java:176)
     at jdk.internal.dynalink.support.CompositeGuardingDynamicLinker.getGuardedInvocation(CompositeGuardingDynamicLinker.java:124)
     at jdk.internal.dynalink.support.LinkerServicesImpl.getGuardedInvocation(LinkerServicesImpl.java:144)
     at jdk.internal.dynalink.DynamicLinker.relink(DynamicLinker.java:232)
     at jdk.nashorn.internal.scripts.Script$\^eval\_$4._L38$_L563$_L1007(<eval>:1010)
         at jdk.nashorn.internal.scripts.Script$\^eval\_$4.scopeCall-14(<eval>)
             at jdk.nashorn.internal.scripts.Script$\^eval\_$4._L38$_L563(<eval>:2503)
                 at jdk.nashorn.internal.scripts.Script$\^eval\_$2.$split(<eval>:563)
                     at jdk.nashorn.internal.scripts.Script$\^eval\_._L38(<eval>)
                         at jdk.nashorn.internal.scripts.Script$\^eval\_._L15(<eval>:34)
                             at jdk.nashorn.internal.scripts.Script$\^eval\_.runScript(<eval>:15)
                                 at jdk.nashorn.internal.runtime.ScriptFunctionData.invoke(ScriptFunctionData.java:498)
                                 at jdk.nashorn.internal.runtime.ScriptFunction.invoke(ScriptFunction.java:206)
                                 at jdk.nashorn.internal.runtime.ScriptRuntime.apply(ScriptRuntime.java:378)
                                 at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:546)
                                 ... 5 more
--- END ERROR ---
|

I tracked the error down in the JQuery code and the error is coming from 
the block:

|setDocument = Sizzle.setDocument = function( node ) {
     var hasCompare,
         doc = node ? node.ownerDocument || node : preferredDoc,
         **parent = doc.defaultView;** // *ERROR*

     // If no document and documentElement is available, return
     if ( doc === document || doc.nodeType !== 9 || !doc.documentElement ) {
         return document;
     }

     ....
|

*Question*:

 1. is it possible to load any third party library within Nashorn ?
 2. does someone tried to load JQuery inside Nashorn ?
 3. does someone have an idea from where this error might come from?

Thanks,
Rémi

-- 
Rémi Barraquand, Phd
VP of Engineering at Carnot-LSI
http://remibarraquand.com




More information about the nashorn-dev mailing list