Solved

Change System.out to append in a JTextArea

Posted on 2004-09-16
5
332 Views
Last Modified: 2008-02-01
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
Comment
Question by:Breadstick
  • 3
5 Comments
 
LVL 13

Accepted Solution

by:
Webstorm earned 50 total points
ID: 12077509
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
 
LVL 13

Expert Comment

by:Webstorm
ID: 12077580
information about com.sun.tools.javac.Main : http://www.javaworld.com/javatips/jw-javatip131.html
0
 
LVL 92

Expert Comment

by:objects
ID: 12080300
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
 
LVL 2

Author Comment

by:Breadstick
ID: 12081039
Thanks to both of you, but Webstorm was faster and a little more helpful in this case.
0
 
LVL 13

Expert Comment

by:Webstorm
ID: 12082988
:-)
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now