Note that this program may seem to hang on the "green threads"
implementation of the Java Virtual Machine on Solaris, because the VM
blocks for reading from the standard input.  Entering lines of input
should make the program continue.


import java.io.*;

class StreamPlugThread extends Thread {

    private InputStream in;
    private OutputStream out;

    public StreamPlugThread(InputStream in, OutputStream out) {
	this.in  = in;
	this.out = out;
    }

    public void run() {
	byte[] buf = new byte[256];
	int count;
	try {
	    while ((count = in.read(buf)) != -1) {
		out.write(buf, 0, count);
		out.flush();
	    }
	} catch (IOException e) {
	}
    }

    public static void plugTogether(InputStream in, OutputStream out) {
	(new StreamPlugThread(in, out)).start();
    }

    public static void plugTogether(OutputStream out, InputStream in) {
	(new StreamPlugThread(in, out)).start();
    }

    public static Process userProg(String cmd)
	throws IOException
    {
	Process proc = Runtime.getRuntime().exec(cmd);
	plugTogether(System.in,  proc.getOutputStream());
	plugTogether(System.out, proc.getInputStream());
	plugTogether(System.err, proc.getErrorStream());
	return proc;
    }

    public static void main(String[] args) {
	if (args.length != 1)
	    error("usage: StreamPlugThread \"command\"");

	try {
	    userProg(args[0]);
	} catch (IOException e) {
	    error("I/O Exception: " + e);
	}
    }

    public static void error(String err) {
	System.err.println("StreamPlugThread: " + err);
	System.exit(1); // non-zero argument means "not good"
    }
}
