简体   繁体   中英

Redirecting stdout of a jar file in java

The scenario is as follows - I have a main java file file1 and a jar file called Minimize . I create an object of a class called MinimizeTable which is defined in the jar . Now, this creation of object causes some lines to be printed to stdout which was defined in the jar. I want to redirect only this output. Apart from this, my main file has many stdout lines which must be printed to stdout itself. Is there anyway of redirecting only what is being printed by the jar file but not the rest? I have defined the situation below -

class file1{
   public static void main(String[] args)
   {
      MinimizedTable M = new MinimizedTable(); //this instantly prints stuff out which I want to be redirected and not printed to stdout.
      System.out.println("Hello"); //This line must be printed to stdout. 
   }
  }

Is there any way of doing this without touching the jar file? Hope my explanation makes sense.

Without any external dependency and with a slight modification of main:

    System.setOut(new FilteredOutput(System.out));

    MinimizedTable M = new MinimizedTable();

    System.out.println("Hello");

And then FilteredOutput something like this:

import java.io.OutputStream;
import java.io.PrintStream;


public class FilteredOutput extends PrintStream {

    public FilteredOutput(OutputStream out) {
        super(out);
    }

    @Override
    public void println(String x) {

        if (Thread.currentThread().getStackTrace()[2].getClassName().equals(MinimizedTable.class.getName())) {

            super.println("From MinimizedTable: "+x);

        } else {

        super.println(x);

        }
    }

    ...

}

You can achieve it with Log4j , something similar to this...

 log4j.logger.abc.xyz.MinimizedTable=OFF

For pure java solution, please refer this SO question .

I came here in a search for redirecting stdout. The response from BCartolo put me on the right track, but only overrides println. A post by Bozho cleverly suggests writing to a ByteArrayOutputStream, but does not provide for flushing the output for newlines. A more general approach is to override write(byte) in an underlying OutputStream.

My application needs to send System.out text to a "show" method. The redirection command is

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

where ShowStream is

/** Text written to a ShowStream is passed to the show method 
 * when a newline is encountered or a flush occurs.
 * CR characters are ignored.
 */
public static class ShowStream extends OutputStream {
    ByteArrayOutputStream buf = new ByteArrayOutputStream(200);
    public ShowStream() {}

    @Override
    public void     close() { flush(); }

    /** show() the buffer contents, if any */
    @Override
    public void     flush() {
        String s = buf.toString();
        buf.reset();
        show(s, bluePainter);
    }

    /** Put characters in a buffer until newline arrives; 
     * then show() the whole
     * @param b Incoming character
     */
    @Override
    public void write(int b) {
        if (b != '\r')   // ignore CR
            buf.write(b);
        if (b == '\n')   // flush after newline
            flush();
    }
}

The full source code is Message.java

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM