这里有几点建议:
- 带空格的输入是否需要被视为单个String(带空格),或者在实际的多个输入中将其标识?如果是第一个选项,我建议在Windows运行时引用它:
ProcessBuilder pb = new ProcessBuilder("cmd.exe", "/c", "run.bat", ""Some Input With Spaces"", ">>", "stdout.txt","2>>", "stderr.txt");- 而不是使用shell将输入重定向到stdout.txt和stderr.txt,为什么不使用Java使用getOutputStream()和getErrorStream()?这是使用Guava IO包的示例。当然,您可能希望将它们放在单独的线程中,需要适当的异常处理等。
InputStream stdout = new BufferedInputStream(proc.getInputStream()); FileOutputStream stdoutFile = new FileOutputStream("stdout.txt"); ByteStreams.copy(stdout, stdoutFile); InputStream stderr = new BufferedInputStream(proc.getErrorStream()); FileOutputStream stderrFile = new FileOutputStream("stderr.txt"); ByteStreams.copy(stderr, stderrFile); stdout.close(); stderr.close(); stdoutFile.close(); stderrFile.close();- 另一个选择,为什么不创建一个
run.bat
将进行重定向的包装器?
@echo off cmd.exe /c run.bat "%1" >> "%2" 2>> "%3"



