Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java output console error message to file?

I have a simple piece of code that outputs console text to a text file in Java:

PrintStream out = new PrintStream(new FileOutputStream("test2_output.txt"));
System.setOut(out);

However I require this text file to contain the error messages that is produced in the console but they are not included.

How do I do this?

like image 707
Dan_Dan_Man Avatar asked Feb 14 '12 01:02

Dan_Dan_Man


2 Answers

Add:

System.setErr(out);

at the end.

like image 142
icyrock.com Avatar answered Oct 04 '22 22:10

icyrock.com


There is also a System.setErr() call to redirect stderr.

like image 32
colbadhombre Avatar answered Oct 04 '22 22:10

colbadhombre