好吧,我嘗試了十幾種不同的方式,但都沒有成功。我想執行一個自定義exe文件並抓取輸出。它從命令提示符運行良好。我得到的「目錄」工作正常,但不是custom.exe。這裏是代碼:ProcessBuilder執行自定義可執行文件
List<String> command = new ArrayList<String>();
command.add("cmd"); // Even removed these two lines
command.add("/c"); // aka hail mary coding.
//command.add("dir");
command.add("custom.exe"); // even tried "c://custom.exe"
String line;
Process p = new ProcessBuilder(command).start();
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((line = input.readLine()) != null) {
System.out.println(line);
}
我根本沒有輸出。如果我把它放在批處理文件中,我會得到輸出。我有一種感覺,它與%PATH%有關。回到它...
編輯 - >所以事實證明,從該自定義的exe輸出轉到錯誤,所以看到發生了什麼我的代碼:
List<String> command = new ArrayList<String>();
command.add(System.getenv("ProgramFiles(x86)") + "\\mydir\\custom.exe";
String line;
ProcessBuilder pb = new ProcessBuilder(command);
pb.redirectErrorStream(true);
Process p = pb.start();
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((line = input.readLine()) != null) {
System.out.println(line);
}
而且它的工作原理是一個該死的熱門人物。 :)
你想用cmd/c實現什麼? – Bringer128
你有沒有試過把「custom.exe」放入命令?您不應該需要「cmd/c」,因爲Java會自動打開流到子進程的stdin,stdout和stderr的流。 – Bringer128
我確實使用了「cmd/c」作爲dir命令,它工作正常(按照我看到的示例)。嘗試使用和不使用「custom.exe」命令。都沒有工作。 – rtfminc