Home » Java programming language

Java ProcessBuilder redirectErrorStream() method with example

ProcessBuilder Class redirectErrorStream() method: Here, we are going to learn about the redirectErrorStream() method of ProcessBuilder Class with its syntax and example.
Submitted by Preeti Jain, on December 13, 2019

Syntax:

    public boolean  redirectErrorStream ();
    public ProcessBuilder  redirectErrorStream (boolean re_err_stm);

ProcessBuilder Class redirectErrorStream() method

  • redirectErrorStream() method is available in java.lang package.
  • redirectErrorStream () method is used to check whether this process builder combines standard error and standard output.
  • redirectErrorStream (boolean re_err_stm) method is used to puts the redirectErrorStream property of this process builder.
  • These methods don't throw an exception at the time of combining error and output stream.
  • These are non-static methods, it is accessible with the class object only and, if we try to access these methods with the class name then we will get an error.

Parameter(s):

  • In the first case, it does not accept any parameter.
  • In the second case, Boolean re_err_stm - This parameter represents the new property.

Return value:

In the first case, the return type of the method is boolean – It returns true then error output will be combined with the standard output so that by using the getInputStream() method of Process class can be read both error and output otherwise it returns false.

In the second case, the return type of the method is ProcessBuilder, it returns this process builder.

Example:

// Java program to demonstrate the example 
// of redirectErrorStream() method of ProcessBuilder class

import java.io.*;
import java.util.*;

public class RedirectErrorStream {
    public static void main(String[] args) throws Exception {
        // Creating an object of List
        List l = new LinkedList();

        // By using add() method to add elements
        l.add("TextPad.exe");
        l.add("notepad.exe");

        // Instantiating ProcessBuilder object
        ProcessBuilder pr_bu = new ProcessBuilder(l);

        // By using redirectErrorStream() method is 
        // to check whether this error stream is redirected
        // or not
        System.out.println("pr_bu.redirectErrorStream() =" + pr_bu.redirectErrorStream());

        // By using redirectErrorStream(boolean re_err_stm) method is not to 
        // redirect the error stream
        pr_bu.redirectErrorStream(false);
        System.out.println("pr_bu.redirectErrorStream(false) =" + pr_bu.redirectErrorStream());

        // By using redirectErrorStream(boolean re_err_stm) //method is to 
        // redirect the error stream
        pr_bu.redirectErrorStream(true);
        System.out.println("pr_bu.redirectErrorStream(true) =" + pr_bu.redirectErrorStream());
    }
}

Output

pr_bu.redirectErrorStream() =false
pr_bu.redirectErrorStream(false) =false
pr_bu.redirectErrorStream(true) =true



Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.