我正在研究用於編譯程序的Java代碼。g ++不能與Java一起工作
所以用戶可以選擇要編譯的文件,然後程序運行自己的g ++。
找上了網,尤其是計算器,我已經決定要使用此代碼:
//Set a FileChoose called fc, got the file path (filePath) and the directory path (dirPath), then:
ProcessBuilder process=null;
try {
process = new ProcessBuilder("g++", filePath, " -o "+dirPath+"/a.out").start();
} catch (IOException ex) {
System.err.println("Error compiling file");
Logger.getLogger(Nuovo.class.getName()).log(Level.SEVERE, null, ex);
System.exit(0);
}
的一點是,它不返回任何錯誤,當我檢查,如果該文件編譯沒有什麼。
有什麼想法?
非常感謝!
閱讀您的過程的錯誤流,以獲取更多信息 – Jens
此代碼不編譯。 'ProcessBuilder.start()'返回一個'Process',但是你把它賦給一個變量'ProcessBuilder'。 –
對不起之前沒有回覆,我一直有點忙。 問題是沒有任何錯誤。 正如@JornVernee所說的問題是我正在將一個Process返回給ProcessBuilder。我通過這種方式解決了這個問題:Process pr; 運行時rt = Runtime.getRuntime(); try { pr = rt。exec(String []);如果exec()函數的參數是一個包含某些東西(取決於需要)的字符串數組,如{「bin/bash」,「-c」,comand} – icosac