// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

public class CaptureOutput {
    public static void main(String[] args) {
        try {
            PrintStream save = System.out;
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            PrintStream pw = new PrintStream(baos);
            System.setOut(pw);
            T t = new T();
            t.start();
            t.join();
            System.setOut(save);
            pw.flush();
            System.out.println("from thread: " + baos.toString());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

class T extends Thread {
    public void run() {
        System.out.println("Hello world");
    }
}
