2015-09-07 63 views
0

我遇到問題。需要將process.getErrorStream(),process.getInputStream()和process.getOutputStream()重定向到JTextPane。 進程位於類A中,且JTextPane位於類B中,因此它們之間沒有直接連接。爲此我創建了界面。所以,我可以調用方法informListener(String message),它將行追加到JTextPane。但我找不到任何解決方案可以解決我的問題。有沒有什麼好的和簡單的解決方案?控制檯提示到JTextPane

謝謝。

+0

使用** **觀察者模式 – Blip

回答

1

你需要的是兩個線程,它們從get*Stream方法返回的輸入流中讀取數據並附加到文本區域。

喜歡的東西:

final BufferedReader reader = new BufferedReader(new InputStreamReader(process.getErrorStream()); 
new Thread(new Runnable() { 
    String line ; 
    while ((line = reader.readLine()) != null) { 
     interfaceObject.informListener(line); 
    } 
}).start(); 

只需確保追加到textPane使用SwingUtilities.invokeLater在EDT發生。

以下程序有效。 (我在OS X上):

package snippet; 

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.util.logging.Level; 
import java.util.logging.Logger; 

public class ProcessOutput { 

    public static void main(String[] args) throws IOException, InterruptedException { 
     final Process p = Runtime.getRuntime().exec("ls -lR"); 

     final BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream())); 
     new Thread(new Runnable() { 
      @Override public void run() { 
       try { 
        String line; 
        while ((line = reader.readLine()) != null) { 
         System.out.println(line); 
        } 
       } catch (IOException ex) { 
       } 
      } 
     }).start(); 

     int waitFor = p.waitFor(); 
     System.out.println(waitFor + " is the return"); 
    } 
} 

檢查您的命令是否正在構建正確。可能只是打印出來,看看你是否能夠從cmdline執行它。

+0

我已經添加了一些代碼樣本的答案評論。不幸的是它不起作用。 – Alex

+0

@Alex可以將其更改爲普通文本窗格並查看它是否有效?我看不到任何代碼問題。 – KDM

+0

什麼意思是「改爲普通文字窗格」? – Alex

0

它不起作用。 InformaListener調用另一種方法。下面是它的代碼:

HTMLEditorKit kit = new HTMLEditorKit(); 
HTMLDocument doc = new HTMLDocument(); 
... 
logTextPane.setEditorKit(kit); 
logTextPane.setDocument(doc); 

public void onLogData(final String message) { 
     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        kit.insertHTML(doc, doc.getLength(), message, 0, 0, null); 
       } catch (BadLocationException ex) { 
        Logger.getLogger(SummaryPanel.class.getName()).log(Level.SEVERE, null, ex); 
       } catch (IOException ex) { 
        Logger.getLogger(SummaryPanel.class.getName()).log(Level.SEVERE, null, ex); 
       } 
      } 
     }); 

    } 

這裏是代碼getErrorStream:

final String exec = "cmd /C start " + batStrPath + "\\upgrade-build.bat"; 
      final Process p = Runtime.getRuntime().exec(exec); 

      final BufferedReader reader = new BufferedReader(new InputStreamReader(p.getErrorStream())); 
      new Thread(new Runnable() { 
       @Override 
       public void run() { 
        try { 
         String line ; 
         while ((line = reader.readLine()) != null) { 
          informListener(line); 
         } 
        } catch (IOException ex) { 
         Logger.getLogger(Installer.class.getName()).log(Level.SEVERE, null, ex); 
        } 
       } 
      }).start(); 

      p.waitFor();