Solved

Change System.out to append in a JTextArea

Posted on 2004-09-16
5
342 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

911 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

22 Experts available now in Live!

Get 1:1 Help Now