title: Call javascript(jQuery/Envjs) from java code
body: <p>I am trying to execute Javascript code from Java. Javascript code uses jquery so I prepend the jquery.js before my code. But it throws following exception,</p>
  
  <pre><code>Exception in thread "main" javax.script.ScriptException: sun.org.mozilla.javascript.internal.EcmaError: ReferenceError: "window" is not defined. (&lt;Unknown source&gt;#1) in &lt;Unknown source&gt; at line number 1
  </code></pre>
  
  <p>As I run this from the Java code, I understand that it does not have access to the window object so above exception. I found that EnvJs provides the implementation for the required environment so I tried to load that first by putting its content first while generating the script content to eval. But run into following exception,</p>
  
  <pre><code>Exception in thread "main" javax.script.ScriptException: sun.org.mozilla.javascript.internal.EcmaError: TypeError: Cannot call property getCurrentContext in object [JavaPackage org.mozilla.javascript.Context]. It is not a function, it is "object". (&lt;Unknown source&gt;#1247) in &lt;Unknown source&gt; at line number 1247
  </code></pre>
  
  <p><br/>Following is the code snippet,</p>
  
  <pre><code>ScriptEngineManager manager = new ScriptEngineManager();
  ScriptEngine engine = manager.getEngineByName("JavaScript");
  
  String script = "Envjs code" + "jQuery code" + "my java script"; //code of envjs + jquery from the link provided at the end
  engine.eval(script);
  
  Invocable inv = (Invocable) engine;
  inv.invokeFunction("myFunc", obj1, obj2);
  </code></pre>
  
  <p><br/>I do not use any browser features so do not require object's like window. So ideally I do not want to load Envjs. Please let me know how to load jQuery code.</p>
  
  <p><br/>One more question - How to pass Json Object from Java code to Javascript function as parameter?</p>
  
  <p><br/><a href="http://www.envjs.com/dist/env.rhino.1.2.js" rel="nofollow">http://www.envjs.com/dist/env.rhino.1.2.js</a>
  <br/><a href="http://code.jquery.com/jquery-1.9.0.min.js" rel="nofollow">http://code.jquery.com/jquery-1.9.0.min.js</a></p>