title: Executing javascript in java - Opening a URL and getting links
body: <pre><code>import javax.script.ScriptEngine;
  import javax.script.ScriptEngineManager;
  import java.io.FileReader;
  
  public class Main {
  
      public static void main(String[] args) {
  
          ScriptEngineManager manager = new ScriptEngineManager();
          ScriptEngine engine = manager.getEngineByName("js");
          try {
              FileReader reader = new FileReader("C:/yourfile.js");
              engine.put("urlfromjava", "http://www.something.com/?asvb");
              engine.eval(reader);
              reader.close();
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
  }
  </code></pre>
  
  <p>Right now, the yourfile.js contains this line</p>
  
  <pre><code>function urlget(url)
  {
      print("URL:"+url);
      var loc = window.open(url);
      var link = document.getElementsByTagName('a')["61"].href;
      return ("\nLink is: \n"+link); 
  
  }
  var x = urlget(urlfromjava);
  print(x);
  </code></pre>
  
  <p>I get the error </p>
  
  <pre><code>"javax.script.ScriptException: sun.org.mozilla.javascript.internal.EcmaError: ReferenceError: "window" is not defined"
  </code></pre>
  
  <p>How to open a URL and get the links of it from java?</p>