23

In UNIX, I'm supposed to write a Java file that will print "EXIT 1" to the standard error, and then exit with a status of 1.

Here is my approach..

System.err.println("EXIT 1");
System.exit(1);

Is this what I'm supposed to do?

If so, how am I supposed to use it in the Unix shells? When I compile and run it in the bash, it just prints "EXIT 1" (so it does the same thing as System.out.println, why should I use "err"?). What is the "standard error" here?

MultiplyByZer0
  • 6,302
  • 3
  • 32
  • 48
Gavin Z.
  • 423
  • 2
  • 6
  • 16
  • Each process can have two output streams, stdout and stderr. These can be filtered so that if you don't care about the "out put" but wanted to log the errors, you can do so - for example... – MadProgrammer Jan 17 '14 at 23:17
  • 1
    "By the way, why cs major needs to learn UNIX?" - Why take CS if you don't want to learn CS? – James Allman Jan 17 '14 at 23:20
  • 1
    Why is this tagged `bash`? This is strictly a Java question. – DopeGhoti Jan 18 '14 at 00:05

2 Answers2

33

Every running program has these three streams:

  • Standard input (stdin), which normally comes from the keyboard. Exposed as System.in
  • Standard out (stdout), which normally goes to the console. Exposed as System.out
  • Standard error (stderr), which normally also goes to the console. Exposed as System.err

Your program is correct – it does print to stderr. But under normal circumstances, the stderr stream goes to the console just like the stdout stream, so they are visually indistinguishable.

However, the reason you should use stderr instead of stdout for error messages, is redirection. That means that you send stderr to a file instead of the console. Meanwhile, stdout will be unaffected, because the two streams are independent.

For example, you can do this in bash, cmd, PowerShell, etc:

$ java Program 2> errors.txt

Now, all output with System.err.println() will end up in errors.txt, while System.out.println() will still go to the screen. This can help with debugging.

MultiplyByZer0
  • 6,302
  • 3
  • 32
  • 48
  • 3
    More to the point, `java Program >normal-output.txt` will run the program, and send the normal output to a file (for later processing), but still send errors to the screen. – Martin Bonner supports Monica Feb 06 '17 at 08:26
  • Isn't it possible to redirect either output(stdout/stderr) to a file? https://askubuntu.com/a/731237 – shaahiin Feb 26 '18 at 06:31
  • @SmS Yes. `>` redirects stdout and `2>` redirects stderr. To redirect both, use `./program >stdout.txt 2>stderr.txt` (two files) or `./program > combined.txt 2>&1` (one file). This syntax works for both bash and cmd. – MultiplyByZer0 Feb 26 '18 at 06:43
1

There are three data streams associated with nearly every process:

  • Standard Input: This is the stream of input into a program, either from a terminal, a console, piped output from another process, or some other means.
  • Standard Error: This is where all debugging and error messages should go. This is so that this sort of information can easily be separately captured from the regular output of a program. Web servers do this, by sending error messages to an error_log file via stderr, while the normal log file would be e. g. access_log.
  • Standard Output: This is the where all typical, expected output that a user running a program should expect to see said output appear.

Standard Output (stdout) and Standard Error (stderr) are nearly always the first and second output streams coming from a process, respectively. This allows me to do something like /path/to/my/neat/program > logs/program.log 2> logs/program.err and have output and errors nicely sorted.

DopeGhoti
  • 757
  • 7
  • 15
  • NB. Not “almost,” but on Unix systems, “always.” FD 0,1,2 are guaranteed to be stdin, stdout, stderr (resp.) – BRPocock Jan 18 '14 at 00:23