Feb 22

We’ve all got code riddled with System.out.println() messages. We should use a logging framework, but often we’re in a rush and we end up writing to stdout because its simpler at the time.

If there are too many System.out.println() calls and we want to stop them going to the console then we can redirect stdout and stderr to a different stream using the setOut() and setErr() methods provided by the System class.

If we want to instead have them written to a Swing text component such as a JTextArea or JTextPane then what we need is an OutputStream subclass that will write to the text component.

The following code shows how that can achieved, resulting in System.out.println() call writing to your designated JTextComponent.

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

import javax.swing.SwingUtilities;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.JTextComponent;

public class RedirectOutput {

	private JTextComponent textComponent;
	
	public static void sendTo(JTextComponent textComponent) {
		new RedirectOutput(textComponent).redirectSystemStreams();
	}
	
	private RedirectOutput(JTextComponent textComponent) {
		this.textComponent = textComponent;
	}
	
	private void updateTextComponent(final String text) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				Document doc = textComponent.getDocument();
				try {
					doc.insertString(doc.getLength(), text, null);
				} catch (BadLocationException e) {
					throw new RuntimeException(e);
				}
				textComponent.setCaretPosition(doc.getLength() - 1);
			}
		});
	}

	private void redirectSystemStreams() {
		OutputStream out = new OutputStream() {
			@Override
			public void write(final int b) throws IOException {
				updateTextComponent(String.valueOf((char) b));
			}

			@Override
			public void write(byte[] b, int off, int len) throws IOException {
				updateTextComponent(new String(b, off, len));
			}

			@Override
			public void write(byte[] b) throws IOException {
				write(b, 0, b.length);
			}
		};

		System.setOut(new PrintStream(out, true));
		System.setErr(new PrintStream(out, true));
	}

}

written by objects \\ tags: , , , , , , ,

Mar 24

You need to read any output from standard output or standard error when running a process using Runtime.exec(). If you don’t the buffers may fill up causing the process to lock up.

The following code can be used to handle reading the process output, and the linked article provides a lot of great insight into the correct usage of Runtime.exec().

/** code copied from
 *  http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html
 *  When Runtime.exec() won't: Navigate yourself around pitfalls
 *  related to the Runtime.exec() method
 *  @author Michael Daconta
 */
import java.util.*;
import java.io.*;

class StreamGobbler extends Thread
{
    InputStream is;
    String type;
    OutputStream os;
    
    StreamGobbler(InputStream is, String type)
    {
        this(is, type, null);
    }

    StreamGobbler(InputStream is, String type, OutputStream redirect)
    {
        this.is = is;
        this.type = type;
        this.os = redirect;
    }
    
    /** creates readers to handle the text created by the external program
    */		
    public void run()
    {
        try
        {
            PrintWriter pw = null;
            if (os != null)
                pw = new PrintWriter(os);
                
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);
            String line=null;
            while ( (line = br.readLine()) != null)
            {
                if (pw != null)
                    pw.println(line);
                System.out.println(type + ">" + line);    
            }
            if (pw != null)
                pw.flush();
        } catch (IOException ioe)
            {
            ioe.printStackTrace();  
            }
    }
}

written by objects \\ tags: , , , , , , ,