This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan 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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
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
Author

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

nish bhai
Greenhorn

Joined: Jul 04, 2003
Posts: 3
Hi,
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

-D<name>=<value>
set a system property

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

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

nish
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Hi,

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
Greenhorn

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.

...Ariel
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.

Layne


Java API Documentation
The Java Tutorial
nish bhai
Greenhorn

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.setErr(stream);
System.err.println("inside the err stream");

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

nish
Jim Yingst
Wanderer
Sheriff

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to set the error stream using "java" option -D
 
Similar Threads
Jikes in Ant
Hibernate with HSQLdb - SQLException requires collation in statement
Mapping document query
Ant usage
Hibernate Setup - Beginner Questions