Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect process output to stdout

Tags:

java

groovy

I would like to execute foo.bat from within a Groovy program and have the resulting process' output redirected to stdout. Either a Java or Groovy code example would be fine.

foo.bat can take several minutes to run and generates a lot of output, so I would like to see the output as soon as it is generated, rather than having to wait until the process has completed before seeing all the output at once.

like image 961
Dónal Avatar asked Nov 13 '09 23:11

Dónal


People also ask

How do I redirect stdout of a running process?

The way we can redirect the output is by closing the current file descriptor and then reopening it, pointing to the new output. We'll do this using the open and dup2 functions. There are two default outputs in Unix systems, stdout and stderr. stdout is associated with file descriptor 1 and stderr to 2.

How do I redirect output from stdout to a file?

Redirecting stdout and stderr to a file: The I/O streams can be redirected by putting the n> operator in use, where n is the file descriptor number. For redirecting stdout, we use “1>” and for stderr, “2>” is added as an operator.

How do I redirect output to console and file?

The > operator replaces the contents of an existing file every time you use it to redirect output. If you want to save the output from multiple commands to a single file, use the >> operator instead. This appends the output of a command to the end of the specified file, if it already exists.


2 Answers

It is simple to redirect all your stream to standard output using inheritIO() method. This will print the output to the stdout of the process from which you are running this command.

ProcessBuilder pb = new ProcessBuilder("command", "argument"); pb.directory(new File(<directory from where you want to run the command>)); pb.inheritIO(); Process p = pb.start(); p.waitFor(); 

There exist other methods too, like as mentioned below. These individual methods will help redirect only required stream.

    pb.redirectInput(Redirect.INHERIT)     pb.redirectOutput(Redirect.INHERIT)     pb.redirectError(Redirect.INHERIT) 
like image 57
Pandurang Patil Avatar answered Sep 20 '22 01:09

Pandurang Patil


This uses a class which reads all output the executed program generates and displays it in it's own stdout.

class StreamGobbler extends Thread {     InputStream is;      // reads everything from is until empty.      StreamGobbler(InputStream is) {         this.is = is;     }      public void run() {         try {             InputStreamReader isr = new InputStreamReader(is);             BufferedReader br = new BufferedReader(isr);             String line=null;             while ( (line = br.readLine()) != null)                 System.out.println(line);             } catch (IOException ioe) {             ioe.printStackTrace();           }     } }  Runtime rt = Runtime.getRuntime(); Process proc = rt.exec("javac"); //output both stdout and stderr data from proc to stdout of this process StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream()); StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream()); errorGobbler.start(); outputGobbler.start(); proc.waitFor(); 
like image 22
jitter Avatar answered Sep 20 '22 01:09

jitter