tags:

views:

76

answers:

3
+1  Q: 

Hide Java Output

Hi Guys!

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

+2  A: 

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.

Bill K
You can't set it to null. It will cause NullPointerException when a System.out.print() function is called. But you can use another output stream.
Neo Adonis
only problem with this method is that it will also redirect my own code's output to defined print stream
EarnWhileLearn
You can still print to the "original" out.
Bill K
+1  A: 

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()));  
John V.
A: 
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!
}
Leo Izen