1

Possible Duplicate:
Capturing stdout when calling Runtime.exec

Here is the question, have the following code,

public void CMD() {
    try
    {            
        Runtime rt = Runtime.getRuntime();

        Process proc = rt.exec("dir");
        InputStream stdin = proc.getInputStream();
        InputStreamReader isr = new InputStreamReader(stdin);
        BufferedReader br = new BufferedReader(isr);
        String line = null;
        System.out.println("<OUTPUT>");
        while ( (line = br.readLine()) != null)
            System.out.println(line);
        System.out.println("</OUTPUT>");
        int exitVal = proc.waitFor();            
        System.out.println("Process exitValue: " + exitVal);
    } catch (Throwable t)
    {
        t.printStackTrace();
    }
}

How do I make it transfer the output into the jTextArea?

Community
  • 1
  • 1
Armani
  • 151
  • 1
  • 2
  • 6
  • 1
    Err, jtextArea.append()? – user207421 Aug 12 '12 at 01:38
  • 2
    *"Here is the question,"* The question I have for you is why is the code using a sledgehammer (`Runtime.exec()`) to crack a wallnut (a file listing that can be done using the `File` class). After that was answered, I would wonder why the code was presenting a list (of anything) in a text area as opposed to a `JList`, `JTable` or `JComboBox`? All in all, your question is confusing. – Andrew Thompson Aug 12 '12 at 02:15
  • you see, I just start using NetBeans and I basically playing around, but the actual target is, I do have an independent Maude console and by pressing button, i am saving the text1 as a Maude Module for an execution in Maude console, plus the other text2 holds the commands for my module, and then after execution procedure terminated, i want to transfer the output in to the textarea3. – Armani Aug 12 '12 at 20:24
  • anyway, i thank everyone who spent their time and commented the question i asked – Armani Aug 12 '12 at 20:25

2 Answers2

2

It's as easy as:

    while ( (line = br.readLine()) != null) {
        textArea.append(line + "\n");
    }
Reimeus
  • 158,255
  • 15
  • 216
  • 276
1

I think it's better to keep the UI separate from processing - the concept is called layering. I would object to seeing markup in this method. Better to keep that separate from actually acquiring the information from the runtime process.

I'd recommend reading up on the newer ways to execute processes.

  1. http://www.javaworld.com/jw-12-2000/jw-1229-traps.html
  2. http://www.java-tips.org/java-se-tips/java.util/from-runtime.exec-to-processbuilder.html
duffymo
  • 305,152
  • 44
  • 369
  • 561