This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
nish bhai
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ravi Pydi
Greenhorn
Posts: 25
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
java Test >error.log works for windows
 
Ariel Ortiz
Ranch Hand
Posts: 121
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3061
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
nish bhai
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic