Solved

Change System.out to append in a JTextArea

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
more than one jdk and one jre 1 50
maven module vs maven project 3 48
java example issue 5 26
Crystal Reports Licensing Questions 4 38
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

820 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