Hide Java Output

I am using an external library. When a call a method of this library then it outputs some text on console. I want to hide this text from console. How is it possible?

Thanks in Advance

3

4 Answers

You can redefine system.out (I believe it is System.setOut()) I believe you can set it to NULL (Corrected--you can NOT set it to NULL), but you can set it to ANY output stream.

I did something interesting with this once. I saved "System.out" then redirected it to my own output stream with code in the "print" method--that method is called whenever anyone prints to the stream.

Every time a line of input came in to this class, I'd create a stack trace, grab the trace and dig down to the method that called the System.out.println() method. At this point I could prepend the line and have instant logger functionality--it even shows the line number.

This was probably quite slow but it could be turned on and off very easily.

I could also do all the filtering you'd like without touching the source code. Sometimes I'd filter on a single package, sometimes I'd filter on a class and sometimes it would be strings starting with "BK:" which would only print out my messages.

Overall it was a lot of fun and trivial to remove ALL debug output for production.

(I do not recommend the get stack trace thing for production code, it really should be quite slow even though I didn't notice it)

// class variable
public static final OutputStream out;
{ out=System.getOutputStream();// I may have the actual name wrong, but it's close System.setOutputStream(new OutputStreamThatDoesNothing());
}

at this point any calls to:

Redirect.out("Hello");

Should act just as calls to System.out did.

Since there is no magic with this, you can also do something like this if you really want to:

OutputStream tmp=System.getOutputStream();
System.setOutpuatStream(nullStream);
callOffensiveLibraryMethod();
System.setOutputStream(tmp);

This would only eliminate output within that call BUT would be Very Bad if your application was multi-threaded.

3
private PrintStream realSystemOut = System.out;
private static class NullOutputStream extends OutputStream { @Override public void write(int b){ return; } @Override public void write(byte[] b){ return; } @Override public void write(byte[] b, int off, int len){ return; } public NullOutputStream(){ }
}
void someMethod(){ System.setOut(new PrintStream(new NullOutputStream()); realSystemOut.println("Hello World!"); //prints Hello World! System.out.println("Hello World!"); //nothing! System.setOut(realSystemOut); System.out.println("Hello World!"); //prints Hello World!
}

For those looking for a 2020 version that also does not require external libraries for the null stream:

PrintStream out = System.out;
System.setOut(new PrintStream(OutputStream.nullOutputStream()));
//
//do stuff with library...
//
System.setOut(out);

Available since Java 11.

Set the System.out to a NullOutputStream. apacahe.commons has one available.

If you want to print it out accordingly you could write a fun hack.

private static final PrintStream SYSTEM_OUT = System.out();
public static void debug(String debug){ SYSTEM_OUT.println(debug);
}

Somehwere else in the code

 System.setOut(new PrintStream(new NullOutputStream())); 

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like