title: Including a JavaScript file during Rhino eval
body: <p>How would one include a script "Bar" from within another script "Foo" that is being evaluated by the Rhino Engine, running in Java.</p>
  
  <p>IE, setup the script engine like this in Java:</p>
  
  <pre><code>ScriptEngineManager manager = new ScriptEngineManager();
  ScriptEngine engine = manager.getEngineByName("javascript");
  BufferedReader br = new BufferedReader(new FileReader(new File("Foo.js")));
  engine.eval(br);
  </code></pre>
  
  <p>... and put the following in Foo:</p>
  
  <pre><code>load(["Bar.js"])
  </code></pre>
  
  <p>According to <a href="https://developer.mozilla.org/en-US/docs/Rhino/Shell?redirectlocale=en-US&amp;redirectslug=Rhino_Shell#load.28.5Bfilename.2C_....5D.29" rel="nofollow">Rhino shell documentation</a>, that's the way to do it.  But when running from Java, it's clearly not implemented:</p>
  
  <pre><code>javax.script.ScriptException: sun.org.mozilla.javascript.internal.EcmaError: ReferenceError: "load" is not defined.
  </code></pre>
  
  <p>@Phillip:</p>
  
  <p>Thanks for the creative response.  Your solution as is doesn't quite work because eval doesn't make the parsed function available unless the Invocable interface is used.  Further, it doesn't work in cases where the Java code is not accessible.  I elaborated on your workaround and created something that will work in pure JS:</p>
  
  <p>First create an engine, exposing the Invocable interface:</p>
  
  <pre><code>var engine = new Packages.javax.script.ScriptEngineManager().getEngineByName("javascript");
  engine = Packages.javax.script.Invocable(engine);
  </code></pre>
  
  <p>Then "load()" the script(s)</p>
  
  <pre><code>engine.eval(new java.io.FileReader('./function.js'));
  </code></pre>
  
  <p>At this point, functions can be evaluated with:</p>
  
  <pre><code>engine.invokeFunction("printHello", null);
  </code></pre>