2012-06-05 40 views
1

當前有麻煩將小窗口批處理控制檯的輸出重定向到日誌文件。我的Java應用程序需要啓動Runtime.exec()調用,而不必等待它完成並仍然記錄輸出。這裏是我的記錄器類:將系統調用輸出重定向到使用Java的文件

public class BatchThreadLogger extends Thread { 
    private Process process; 
    private String logFilePath; 
    private static final Logger logger = Logger.getLogger(BatchThreadLogger.class); 

    public BatchThreadLogger(Process process, String logFilePath) { 
    this.process = process; 
    this.logFilePath = logFilePath; 
    } 

    public void run() { 
    try { 
     // create logging file 
     File file = new File(logFilePath); 
     file.createNewFile(); 

     // create a writer object 
     OutputStream os = new FileOutputStream(file); 
     PrintWriter pw = new PrintWriter(os); 

     // catch the process output in an InputStream 
     InputStreamReader isr = new InputStreamReader(process.getInputStream()); 
     BufferedReader br = new BufferedReader(isr); 

     // wait for the process to complete 
     int processStatus = process.waitFor(); 

     // redirect the output to the log file 
     String line = null; 
     while ((line = br.readLine()) != null) { 
     pw.println(line); 
     } 

     // add a small message with the return code to the log 
     pw.println("********************************************"); 
     pw.println("********************************************"); 
     pw.println("Batch call completed with return status " + processStatus); 

     pw.flush(); 
     os.close(); 
    } 
    catch (IOException e) { 
     logger.error("IOException raised during batch logging on file " + logFilePath, e); 
    } 
    catch (InterruptedException e) { 
     logger.error("InterruptedException raised during batch process execution", e); 
    } 
    } 
} 

我的電話很簡單:

Process process = Runtime.getRuntime().exec(command); 
BatchThreadLogger logger = new BatchThreadLogger(process, logFilePath); 
logger.start(); 

我的命令,目前只是叫我test.bat的兩個參數。我的測試一批現在只是做:

echo "BATCH CALLED WITH PARAMETER %1 AND %2" 
exit 

我的日誌文件,但確實只包含:

******************************************** 
******************************************** 
Batch call completed with return status 0 

我嘗試過放置waitFor()電話和代碼將輸出重定向到日誌文件之後,沒有成功。我總是看到該命令的黑屏正在啓動,但沒有在日誌...

任何幫助將不勝感激,我失去了一些東西,但無法理解......

回答

1

你不從您創建的過程的標準錯誤讀取。

我懷疑有一條錯誤消息正在寫入標準錯誤,並且由於您只是從標準輸出讀取,所以您沒有收到此錯誤。

我會建議用ProcessBuilder更換您的Runtime.getRuntime().exec(...)使用,使用類似以下內容:

ProcessBuilder pb = new ProcessBuilder("cmd.exe", "/c", "test.bat", "One", "Two"); 
pb.redirectErrorStream(true); 
Process process = pb.start(); 

pb.redirectErrorStream(true);重定向進程的標準錯誤到其標準輸出,讓您不必在兩個獨立的線程中從兩個流中讀取(標準輸出和標準錯誤)。

+0

我不確定你的答案。我想知道如何在批處理文件中回顯文本可以被視爲錯誤,從而指向流程執行的錯誤流。可以肯定的是,我用'getErrorStream()'替換了'getInputStream()'調用,並且它仍然無效...日誌文件中沒有任何內容... – Wis

+0

我還添加了一個隨機的'ping www.google。在回聲調用後的ch',只是爲了確保回聲輸出不會重定向到一個特殊的流或像這樣的任何技巧...在日誌中仍然沒有。順便說一句,我發佈的代碼在Linux上工作。關於您的ProcessBuilder命題,我會看看它是否可以輕鬆替換'Runtime.exec()'調用。 – Wis

+0

我想象一下,當嘗試運行批處理文件時,可能會出現錯誤,該文件由於權限問題而不存在或無法讀取。然而,沒有輸出是很奇怪的。你介意準確地分享你的'命令'變量中的內容嗎?另外什麼類型的應用程序(如命令行,網絡應用程序,圖形用戶界面,服務)是這部分? –

相關問題