• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

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

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
java Test >error.log works for windows
 
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
The knights of nee want a shrubbery. And a tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic