Search code examples
javawindowshttp-redirecterror-handlingout

What is "2" in command, while redirecting both System.out , System.err Java messages to file


I just figured that, we can redirect both System.out and System.err messages to a file, am not able to understand this command which does redirecting while running this below file using : java StandarProgram> output.log 2>error.log

What is 2? and if am not using 2, error messages are displayed on console and not redirected to file? am not able to get this at all.

public class StandardProgram{

    public static void main(String[] args){

       System.out.println("Hello");
       System.err.println("World");

    }
}

Solution

  • 2 is a standard error file descriptor.

    File descriptor 1 is the standard output (stdout). File descriptor 2 is the standard error (stderr).

    2>error.log 
    

    is used to redirect the standard error logs to a file named error.log