2015-09-07 42 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();