This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes how to set the error stream using Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to set the error stream using "java" option -D" Watch "how to set the error stream using "java" option -D" New topic

how to set the error stream using "java" option -D

nish bhai

Joined: Jul 04, 2003
Posts: 3
i'm trying to use the System.err.println("...."); in my program in a catch block, but how do i specify the error stream in the java command when i invoke my program. by default, if i don't mention anything, i suppose the error stream is the same as the output stream. But i want to log into a file.. like an error log file.
this is the instruction given by the java command help

set a system property

can anyone pls identify the correct name of the error stream property?

i tried this to run my java program "",
java -Derror=error.log Test.. one of my shots in the dark.. but no good. :-)

Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24187


Welcome to JavaRanch!

The "java" launcher program has no built-in provision for redirecting System.out or System.err to a file. You can use your operating system's facilities; for example, on UNIX this would look like

java Foo 2>error.log

[Jess in Action][AskingGoodQuestions]
Ravi Pydi

Joined: Sep 01, 2002
Posts: 25
java Test >error.log works for windows
Ariel Ortiz
Ranch Hand

Joined: May 14, 2004
Posts: 121
You can also use the System.setErr(PrintStream) static method. I found this to be useful in Windows when you want to send stderr to a file but ordinary stdout to the console.

Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Ravi Pydi:
java Test >error.log works for windows

I believe this will only send output from System.out to the file but not the output from System.err.


Java API Documentation
The Java Tutorial
nish bhai

Joined: Jul 04, 2003
Posts: 3
Hi, Thanks a ton for all the quick replies. I tried out your suggestions and got the thing.
java Test > <filename> only redirects the System.out stream to the file given.
And thankyou Ariel, i used your idea.
PrintStream stream = new PrintStream(new FileOutputStream("err.log"));
System.err.println("inside the err stream");

all the System.err.print statements do get printed in the err.log file.

Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
Note that's EFH's command

should work on most Windows machines as well as Unix, directing the error stream (but not standard output) to the file error.log.

There's another form which directs both standard out and errors to the same place:

Again, this should work on both Unix and Windows. Here the 1>output.log directs standard out to output.log, and 2>&1 directs errors to the same place as 1, the standard out. Of course both "error.log" and "output.log" can be replaced with whatever filenames you prefer.
[ July 31, 2005: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
GeeCON Prague 2014
subject: how to set the error stream using "java" option -D