I am running a java program from a OS X 10.8 bash terminal, and are trying to rederect the output it is producing.

However, when either running this throug a pipe, or rederecting it to a file, the output is blank, however I see the output in the terminal.

To illustrate this:

> java program.java
13/10/02 14:18:30 WARN some
13/10/02 14:18:30 INFO log
13/10/02 14:18:30 INFO messages

> java program.java > log
> cat log

Can the java program be set up so that it is writing to another stream than stdout, but a stream that still produces output in the terminal. Is such a thing possible?

  • 1
    Have you tried redirecting stderr? – Kotte Oct 2 '13 at 12:53

There are three standard files open for each program, stdin (standard input), stdout(standard output), and stderr (standard error). Writes to both stdout and stderr is output in the terminal by default.

It is a common convention to write errors and log messages to stderr instead of stdout in order to not mix log or error messages with actual program output. You can redirect stderr using 2>, for instance:

command 2> log
  • Cool, worked. I tried tail -f /dev/stderr before, but that did not show anything, which led me to dismiss that it was writing to stderr. Shouldn't /dev/stderr have picked it up? – Theodor Oct 2 '13 at 13:00
  • 4
    /dev/stderr is typically a symlink to /proc/self/fd/2. Your tail invocation tries to to read the stderr of the tail process itself, which of course yields no output. – Thomas Nyman Oct 2 '13 at 13:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.