Reputation: 14060
Is there a way to set the stream System.err so everything written to it is ignored? (i.e. discarded not outputted)
Upvotes: 22
Views: 8124
Reputation: 1
The other Answers aren't incorrect.
I think with the use of the nullOutputStream()
Function from the PrintStream
Class it gets easier to understand what is happenening with less overhead.
System.setErr(new PrintStream(PrintStream.nullOutputStream()));
If you want to restore the original Error Stream later save it before overriding it:
PrintStream origStream = System.err;
System.setErr(new PrintStream(PrintStream.nullOutputStream()));
// Do your Sutff here which is printing to System.err
System.setErr(origStream);
Upvotes: 0
Reputation: 7774
Using commons-io's NullOutputStream, just:
System.setErr(new PrintStream(new NullOutputStream()));
Upvotes: 0
Reputation: 533740
You can use System.setErr() to give it a PrintStream which doesn't do anything.
See @dogbane's example for the code.
Upvotes: 18
Reputation: 4715
Just set Error to dommy implementation:
System.setErr(new PrintStream(new OutputStream() {
@Override
public void write(int arg0) throws IOException {
// keep empty
}
}));
You need to have special permission to do that.
RuntimePermission("setIO")
Upvotes: 5
Reputation: 8623
You could redirect the err Stream to /dev/null
OutputStream output = new FileOutputStream("/dev/null");
PrintStream nullOut = new PrintStream(output);
System.setErr(nullOut);
Upvotes: 1
Reputation: 274778
System.setErr(new PrintStream(new OutputStream() {
public void write(int b) {
}
}));
Upvotes: 44