2011-04-17 103 views
2

如何在java中顯示jtextarea上的shell命令輸出?如何在java中的jtextarea上顯示shell命令輸出?

請幫助 感謝

+1

請有看看回答你的問題的兩個不錯的鏈接:[Redirect-output-stderr-stdout-JTextArea](http://www.coderanch.com/t/458147/GUI/java/Redirect-output-stderr-stdout-JTextArea)和這個經典:[當Runtime.exec()不會](http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html)。希望這可以幫助! – 2011-04-17 13:43:48

回答

2

你可以得到下面的代碼片段shell命令的輸出,在while循環設置的JTextArea。

try { 
     String cmd = "java"; // Set shell command 
     Process child = Runtime.getRuntime().exec(cmd); 

     InputStream lsOut = child.getInputStream(); 
     InputStreamReader r = new InputStreamReader(lsOut); 
     BufferedReader in = new BufferedReader(r); 

     // read the child process' output 
     String line; 
     while ((line = in.readLine()) != null) { 
      System.out.println(line); 
      // You should set JtextArea 
     } 
    } catch (Exception e) { // exception thrown 

     System.err.println("Command failed!"); 

    } 
+0

我認爲你的代碼示例需要改變來處理Swing線程問題。 – 2011-04-17 13:48:33

2

您需要讀取標準輸出作爲進程輸入流並使用另一個線程的事件隊列更新JTextArea。請參見下面的示例代碼:

public class OutputDisplayer implements Runnable { 

protected final JTextArea textArea_; 
protected Reader reader_ = null; 

public OutputDisplayer(JTextArea textArea) { 
    textArea_ = textArea; 
} 

public void commence(Process proc) { 
    InputStream in = proc.getInputStream(); 
    reader_ = new InputStreamReader(in); 
    Thread thread = new Thread(this); 
    thread.start(); 
} 

public void run() { 
    StringBuilder buf = new StringBuilder(); 
    try { 
     while(reader_ != null) { 
      int c = reader_.read(); 
      if(c==-1) return; 

      buf.append((char) c); 
      setText(buf.toString()); 
     } 
    } catch (IOException ioe) { 
     buf.append("\n\nERROR:\n"+ioe.toString()); 
     setText(buf.toString()); 
    } finally { 
     try { 
      reader_.close(); 
     } catch (IOException ioe) { 
      ioe.printStackTrace(); 
     } 
    } 
} 


private void setText(final String text) { 
    EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      textArea_.setText(text); 
     } 
    }); 
} 
} 
+0

調用.setText()似乎消耗大量內存。請看這個,因爲我正在請求幫助。謝謝。 http://stackoverflow.com/questions/11302982/jtextarea-consumes-a-lot-of-memory – jobobo 2012-07-03 07:52:31

2

除了在我的評論我的兩個環節上面,我創建並使用了TextAreaOutputStream,有助於輸出流數據重定向到一個textarea:

import java.io.IOException; 
import java.io.OutputStream; 

import javax.swing.JTextArea; 
import javax.swing.SwingUtilities; 

public class TextAreaOutputStream extends OutputStream { 

    private final JTextArea textArea; 
    private final StringBuilder sb = new StringBuilder(); 
    private String title; 

    public TextAreaOutputStream(final JTextArea textArea, String title) { 
     this.textArea = textArea; 
     this.title = title; 
     sb.append(title + "> "); 
    } 

    @Override 
    public void flush() { 
    } 

    @Override 
    public void close() { 
    } 

    @Override 
    public void write(int b) throws IOException { 

     if (b == '\r') 
     return; 

     if (b == '\n') { 
     final String text = sb.toString() + "\n"; 
     SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       textArea.append(text); 
      } 
     }); 
     sb.setLength(0); 
     sb.append(title + "> "); 
     } 

     sb.append((char) b); 
    } 
} 
+0

+1非常好的例子。 – Boro 2011-04-17 14:16:18

+0

啊,這總是使用Latin-1編碼。更好地實現一個TextAreaWriter像你的代碼,並將其包裝到轉換編碼(WriterOutputStream)或從流程讀取時解碼的東西。 – 2011-04-17 14:58:43

+0

像這樣的東西似乎有內存問題。如果您有機會,請在此處查看內存消耗問題: http://stackoverflow.com/questions/11302982/jtextarea-consumes-a-lot-of-memory – jobobo 2012-07-03 07:50:53

相關問題