Nashorn integration with third party libraries

Rémi Barraquand dev at
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(
at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(
at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(
at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(
at jdk.nashorn.api.scripting.NashornScriptEngine.eval(
at javax.script.AbstractScriptEngine.eval(
at io.dahuapp.cli.CLI.main(
Caused by:<eval>:1010 TypeError: Cannot read property "defaultView" from undefined
     at jdk.nashorn.internal.runtime.ECMAErrors.error(
     at jdk.nashorn.internal.runtime.ECMAErrors.typeError(
     at jdk.nashorn.internal.runtime.ECMAErrors.typeError(
     at jdk.nashorn.internal.runtime.ECMAErrors.typeError(
     at jdk.nashorn.internal.runtime.Undefined.lookupTypeError(
     at jdk.nashorn.internal.runtime.Undefined.lookup(
     at jdk.nashorn.internal.runtime.linker.NashornLinker.getGuardedInvocation(
     at jdk.internal.dynalink.DynamicLinker.relink(
     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(
                                 at jdk.nashorn.internal.runtime.ScriptFunction.invoke(
                                 at jdk.nashorn.internal.runtime.ScriptRuntime.apply(
                                 at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(
                                 ... 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;



 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?


Rémi Barraquand, Phd
VP of Engineering at Carnot-LSI

More information about the nashorn-dev mailing list