title: Executable Failing When Called From ProcessBuilder, But Not From Command-Line
body: <p>I'm trying to run a simple image-capture script using <a href="http://phantomjs.org/" rel="nofollow"><strong>PhantomJS</strong></a> from within my Java program. The end goal is to automate the process of taking screenshots of specific webpages.</p>
  
  <p>My problem is that when I run the following code, the program exits with a return value of -1 and no image is produced:</p>
  
  <pre><code>Process process = new ProcessBuilder("C:\\PhantomJS\\phantomjs.exe", "imagecap.js", "http://www.google.com", "google.png").start();
  process.waitFor();
  System.out.println("Program exited with: " + process.exitValue());
  </code></pre>
  
  <p>However, when I run the equivalent command from the windows command prompt:</p>
  
  <pre><code>C:\PhantomJS\phantomjs.exe imagecap.js http://www.google.com google.png
  </code></pre>
  
  <p>It runs fine and produces the correct .png file.</p>
  
  <hr>
  
  <p>I've also tried having Java run the command through cmd.exe as Administrator with no luck:</p>
  
  <pre><code>Process process = Runtime.getRuntime().exec("runas /profile /user:Administrator \"cmd /c start /wait" +
      "C:\\PhantomJS\\phantomjs.exe imagecap.js http://www.google.com google.png\"");
  </code></pre>
  
  <p>The above code returns 1 instead of -1 like the first example, although I have no idea why.</p>
  
  <hr>
  
  <p>The tiny Javascript script I'm running through <a href="http://phantomjs.org/" rel="nofollow"><strong>PhantomJS</strong></a> is:</p>
  
  <pre><code>var page = require('webpage').create();
  page.viewportSize = { width: 1600, height: 1024 };
  var system = require('system');
  
  page.open(system.args[1], function() {  
      page.render(system.args[2])
      phantom.exit();
  });
  </code></pre>
  
  <p>I absolutely know the script works, I can run it manually and it works flawlessly.</p>
  
  <p>I'd like to know if anyone can explain to me why my attempts to run this using Java are failing.</p>