2013-05-27 19 views
0

我想從java運行C++ .exe文件,並且還想打印.exe file.i的輸出嘗試併成功從java運行C++ .exe文件,但是我沒有得到我怎樣才能使用Java打印C++ .exe文件的輸出(在Java輸出場),我試圖使用processExitValue和WAITFOR方法,但沒有得到期望的output.The java代碼是這裏在java中打印C++ .exe文件的輸出

int processExitVal = 0; 
     try { 
     Process p = Runtime.getRuntime().exec("cmd /c start rs.exe"); 
     processExitVal = p.waitFor(); 
     // p.getOutputStream(); 
     //InputStreamReader ir=new InputStreamReader(p.getInputStream()); 
     //BufferedReader t = new BufferedReader((new InputStreamReader(p.getInputStream()))); 
     // int y=Integer.parseInt(t.readLine()); 
     InputStream in=p.getInputStream(); 
     System.out.println(in.read()); 
     //System.out.println("output"+Process.getOutputStream()); 
     } catch (IOException e) { 
     System.out.println("IOException"); 
     e.printStackTrace(); 
     } catch (InterruptedException e) { 
     System.out.println("InterruptedException"); 
     e.printStackTrace(); 
     } 
     System.out.println(processExitVal); 
     System.out.println("Execution complete"); 
} 

我將感謝如果你能幫我解決這個問題。在此先感謝

+0

甲開始與'getOutputStream'取代'getInputStream'。 – Deestan

+0

'getInputStream'應該沒問題 – displayname

+0

閱讀(並實現)*所有的*當[Runtime.exec()將不會](http://www.javaworld.com/jw-12-2000/jw-1229 -traps.html)。這可能會解決問題。如果不是,它應該提供更多關於失敗原因的信息。然後忽略它引用'exec'並使用'ProcessBuilder'構建'Process'。還要將'String arg'分解爲'String [] args'來解釋其本身包含空格的參數。 –

回答

0

您可以使用Scanner,然後從中讀取行。你確定你的程序正在寫標準輸出嗎?

編輯:

讀():讀取數據的從輸入流的下一個字節。

你必須使用一個掃描儀:

Scanner scan = new Scanner(p.getInputStream()); 
String line = scan.getNextLine(); 

關於Deestan的回答是:getInputStream()是正確的方法在這裏,我們要處理的輸出,這對應用程序的輸入。

+0

與掃描儀相同的情況下,它也不會work.please告訴我,如果我需要做一些改變,在C++從主method.process返回一些值執行罰款,我不知道該進程的標準輸出 – JohnH

0

使用 「rs.exe」 而不是 「CMD/C開始rs.exe」

作爲例子:

Process process = Runtime.getRuntime().exec("rs.exe"); 
BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream())); 
String line = null; 
while((line = in.readLine()) != null) { 
    System.out.println(line); 
} 
+0

cmd/c啓動rs.exe是好的,rs.exe不會工作,BufferedReader我已經使用過,如果你能看到我的代碼中的評論,建議別的東西請:) – JohnH