Java ProcessBuilder redirectErrorStream() Method



Description

The Java ProcessBuilder redirectErrorStream() method tells whether this process builder merges standard error and standard output. If this property is true, then any error output generated by subprocesses subsequently started by this object's start() method will be merged with the standard output, so that both can be read using the Process.getInputStream() method. This makes it easier to correlate error messages with the corresponding output. The initial value is false.

Declaration

Following is the declaration for java.lang.ProcessBuilder.redirectErrorStream() method

public boolean redirectErrorStream()

Parameters

NA

Return Value

This method returns this process builder's redirectErrorStream property

Exception

NA

Getting redirect error stream details for Notepad from a Process Builder Example

The following example shows the usage of ProcessBuilder redirectErrorStream() method. In this program, we've created an array of Strings and added notepad.exe and test.txt to it. Using that list, we've initialized a ProcessBuilder instance. Now using redirectErrorStream() method, we've retrieved the redirect error stream and printed a corresponding mapped flag.

package com.tutorialspoint;

public class ProcessBuilderDemo {

   public static void main(String[] args) {

      // create a new list of arguments for our process
      String[] list = {"notepad.exe", "test.txt"};

      // create the process builder
      ProcessBuilder pb = new ProcessBuilder(list);

      // check if errorstream is redirected
      System.out.println(pb.redirectErrorStream());
   }
}

Output

Let us compile and run the above program, this will produce the following result −

false

Getting redirect error stream details for Calculator from a Process Builder Example

The following example shows the usage of ProcessBuilder redirectErrorStream() method. In this program, we've created an array of Strings and added calc.exe to it. Using that list, we've initialized a ProcessBuilder instance. Now using redirectErrorStream() method, we've retrieved the redirect error stream and printed a corresponding mapped flag.

package com.tutorialspoint;

public class ProcessBuilderDemo {

   public static void main(String[] args) {

      // create a new list of arguments for our process
      String[] list = {"calc.exe"};

      // create the process builder
      ProcessBuilder pb = new ProcessBuilder(list);

      // check if errorstream is redirected
      System.out.println(pb.redirectErrorStream());
   }
}

Output

Let us compile and run the above program, this will produce the following result −

false

Getting redirect error stream details for Windows Explorer from a Process Builder Example

The following example shows the usage of ProcessBuilder redirectErrorStream() method. In this program, we've created an array of Strings and added explorer.exe to it. Using that list, we've initialized a ProcessBuilder instance. Now using redirectErrorStream() method, we've retrieved the redirect error stream and printed a corresponding mapped flag.

package com.tutorialspoint;

public class ProcessBuilderDemo {

   public static void main(String[] args) {

      // create a new list of arguments for our process
      String[] list = {"explorer.exe"};

      // create the process builder
      ProcessBuilder pb = new ProcessBuilder(list);

      // check if errorstream is redirected
      System.out.println(pb.redirectErrorStream());
   }
}

Output

Let us compile and run the above program, this will produce the following result −

false
java_lang_processbuilder.htm
Advertisements