2013-02-08 70 views
0

比方說,我都推出了簡單的Java應用程序,輸出一些字符串非標準控制檯中使用以下命令:需要收集控制檯應用程序的輸出,並將其存儲

Runtime.getRuntime().exec("Path:/to/app.exe"); 

我需要的是收集所有數據,啓動應用程序拋出到控制檯。可能嗎?謝謝。

Paul。

+0

類似問題http://stackoverflow.com/questions/3468987/executing-another-application-from-java – 2013-02-08 17:24:33

回答

1

您可以使用ProcessBuilder並獲得其IutputStream。下面是簡單的例子:

public static void main(String[] args) throws Exception { 
    String[] processArgs = new String[]{"ping","google.com"}; 
    Process process = new ProcessBuilder(processArgs).start(); 

    BufferedReader in = new BufferedReader(new InputStreamReader(
      //I'am using Win7 with PL encoding in console -> "CP852" 
      process.getInputStream(), "CP852")); 

    String line; 
    while ((line = in.readLine()) != null) 
     System.out.println(line); 

    in.close(); 
    System.out.println("process ended"); 
} 
+0

非常感謝你。 – 2013-02-08 17:30:08

相關問題