Search code examples
javaubuntu-14.04shellexecute

java getRuntime().exec() does not work


I am trying to use Java exec to run my Script and get the result and use it in my program.

I have following lines of code.

public static void main(String[] argv) throws Exception {

    Process p = Runtime.getRuntime().exec("phantomjs quebecPhantom.js");

    BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));

    String line = in.readLine();

    System.out.println(" [x] Sent '" + line + "'");
}

I expect my PhantomJS Script to be executed and the line variable take the output of the Script. But, my script does not execute (It should take more time and it should produce some screen shots which I don't see). and then, the line variable takes the null value.

Should I consider anything more to have my Script executed?


Solution

  • The problem with the above lines of code is that I needed to get the path to the phantomjs and also to the script in the command I want to execute. then the line should be changed as follows,

    Process p = Runtime.getRuntime().exec("path to phantomjs" + " " + "path to the script");
    

    In my case that would be like

    Process p = Runtime.getRuntime().exec("/usr/bin/phantomjs /home/name/quebecPhantom.js");