我試圖從Java代碼運行命令行。使用Java在文件中從命令行寫入結果
public void executeVcluto() throws IOException, InterruptedException {
String command = "cmd /c C:\\Users\\User\\Downloads\\program.exe C:\\Users\\User\\Downloads\\file.txt 5 >> C:\\Users\\User\\Downloads\\result.txt";
Process process = Runtime.getRuntime().exec(command);
process.waitFor();
if (process.exitValue() == 0) {
System.out.println("Command exit successfully");
} else {
System.out.println("Command failed");
}
}
但是,輸出結果應寫入result.txt的文件未創建。當我在Windows上從cmd執行該命令時,將創建文件並將結果寫入其中。我得到命令退出成功消息。有人能幫助我嗎?
+1過程建設者和重定向 – MadProgrammer