Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 380
  • Last Modified:

Change System.out to append in a JTextArea

I do a lot of java programming while I'm at school, on my laptop.  However, my laptop is old, slow, and has very little room left on the hard drive.  I've tried using several IDE's on it, but they all either run too slow, take up too much disk space, or are complicated and hard to learn how to use.

I'm trying to develop a very simple IDE for personal use, and am stuck in the conceptual stages because I'm not sure how to implement my design.  I'll be using com.sun.tools.javac.Main to compile, and I'm assuming that I'll just use Runtime.exec() to run the programs.  The problem is that I don't know how to redirect standard output from a process like that.
0
Breadstick
Asked:
Breadstick
  • 3
1 Solution
 
WebstormCommented:
Hi Breadstick,

Here is the class i use when i want to redirect the stdout stream of a process :

public class RunProcess
{
    private Process p=null;
    private static final int BUFFER_SIZE=8192;
    private Thread th_in=null,th_out=null,th_err=null;
    private int ret_code=-1;

    public RunProcess(
        final Process p,
        final InputStream p_in)
    {
        this.p=p;

        // Process. OUT
        (th_out=new Thread(){
            byte b[]=new byte[BUFFER_SIZE];
            InputStream inp=p.getInputStream();
            public void run()
            {
                try{
                    for(;;)
                    {
                        int sz=inp.read(b);
                        textarea.append(new String(b,0,sz)); // <--- append stdout to the textarea
                    }
                }catch(Exception e){}
            }
        }).start();

        // Process. ERR
        (th_err=new Thread(){
            byte b[]=new byte[BUFFER_SIZE];
            InputStream inp=p.getErrorStream();
            public void run()
            {
                try{
                    for(;;)
                    {
                        int sz=inp.read(b);
                        textarea.append(new String(b,0,sz)); // <--- append stderr to the textarea
                    }
                }catch(Exception e){}
            }
        }).start();

        if (p_in!=null)
        // Process. IN
        (th_in=new Thread(){
            byte b[]=new byte[BUFFER_SIZE];
            OutputStream outp=p.getOutputStream();
            public void run()
            {
                try{
                    for(;;)
                    {
                        int sz=p_in.read(b);
                        outp.write(b,0,sz);
                    }
                }catch(Exception e){}
            }
        }).start();

        (new Thread(){
            public void run()
            {
                try{
                    ret_code=p.waitFor();
                }catch(Exception ex){}
                if (th_in!=null) th_in.interrupt();
                if (th_out!=null) th_out.interrupt();
                if (th_err!=null) th_err.interrupt();
            }
        }).start();
    }

    public int getReturnCode()
    {
        return ret_code;
    }

    public int waitFor() throws Exception
    {
        return p.waitFor();
    }

    public void destroy()
    {
        p.destroy();
    }
}


You use it like that:

  RunProcess rp=new RunProcess(Runtime.exec(...), your_input_stream);
  // your_input_stream can be null if no input is expected

0
 
WebstormCommented:
information about com.sun.tools.javac.Main : http://www.javaworld.com/javatips/jw-javatip131.html
0
 
objectsCommented:
The following example writes a process's output to a JTextArea, it may be helpful to you:

http://www.objects.com.au/java/examples/util/SwingExec.do
0
 
BreadstickAuthor Commented:
Thanks to both of you, but Webstorm was faster and a little more helpful in this case.
0
 
WebstormCommented:
:-)
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now