8

For some terminal commands, they repeatedly output. For example, for something that's generating a file, it may output the percent that it is complete.

I know how to call terminal commands in Java using

Process p = Runtime.getRuntim().exec("command goes here");

but that doesn't give me a live feed of the current output of the command. How can I do this so that I can do a System.out.println() every 100 milliseconds, for example, to see what the most recent output of the process was.

CodeGuy
  • 28,427
  • 76
  • 200
  • 317
  • Of course, it allows access to STDOUT and STDERR. See http://docs.oracle.com/javase/7/docs/api/java/lang/Process.html – home Feb 16 '13 at 21:30
  • Thanks for that. So, can you show an example of how to use this to get the output every 100 milliseconds or so? – CodeGuy Feb 16 '13 at 21:30
  • 1
    See http://stackoverflow.com/questions/3643939/java-process-with-input-output-stream – home Feb 16 '13 at 21:32
  • Required reading for anyone using Runtime.exec() http://www.javaworld.com/jw-12-2000/jw-1229-traps.html – dnault Feb 16 '13 at 21:37

1 Answers1

11

You need to read InputStream from the process, here is an example:

Edit I modified the code as suggested here to receive the errStream with the stdInput

ProcessBuilder builder = new ProcessBuilder("command goes here");
builder.redirectErrorStream(true);
Process process = builder.start();
InputStream is = process.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));

String line = null;
while ((line = reader.readLine()) != null) {
   System.out.println(line);
}

For debugging purpose, you can read the input as bytes instead of using readLine just in case that the process does not terminate messages with newLine

Community
  • 1
  • 1
iTech
  • 18,192
  • 4
  • 57
  • 80
  • I tried this, but it seems to freeze at in.readLine() (it doesn't output anything, but doesn't end the while loop) – CodeGuy Feb 16 '13 at 21:33
  • 1
    Maybe the command you're running doesn't output any \r or \n characters (which would signal the BufferedReader that it has read a complete line)? Maybe it doesn't generate any output in certain terminal modes (try piping the result to 'more' and see what it outputs)? Maybe the command writes its output to stderr instead of stdout? – dnault Feb 16 '13 at 21:36
  • It will freeze waiting for input, so either your processes does not terminate the data with `newline` or it does not write to the `stdout` at all – iTech Feb 16 '13 at 21:38
  • @dnault see the code I posted here for details of the Java code I tried: http://stackoverflow.com/questions/14915290/best-way-to-call-terminal-command-repeatedly#comment20924963_14915290 – CodeGuy Feb 16 '13 at 21:40
  • @CodeGuy Try exec'ing mencoder directly. Also, specify the command as a String[] instead of a single String (in case any of the arguments contain spaces). – dnault Feb 16 '13 at 21:53
  • @dnault then I get: Cannot run program "mencoder -ss 0 -endpos 60 /Users/MyFolder/MyVideo.avi -oac copy -ovc copy -o /Users/MyFolder/output.avi": error=2, No such file or directory – CodeGuy Feb 16 '13 at 21:57
  • is /Users/MyFolder/output.avi the absolute path or the relative from the pwd? – daniel Feb 16 '13 at 22:45
  • @CodeGuy you may need to specify the full path to the mencoder executable. – dnault Feb 19 '13 at 21:07
  • i have tried this but using bufferedreader we actually dont get the terminal text line by line(realtime terminal text),it copies the whole text obtained after executing the terminal command and displays it in one step,any one has any idea how to get realtime text character by character from inputstream? – nikhil kekan Feb 28 '14 at 16:54