I have the following windows batch file (run.bat):
@echo off
echo hello batch file to sysout
And the following java code, which runs the ba
Several suggestions here:
ProcessBuilder pb = new ProcessBuilder("cmd.exe", "/c",
"run.bat", "\"Some Input With Spaces\"",
">>", "stdout.txt","2>>", "stderr.txt");
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
wrapper that will make the redirections?@echo off
cmd.exe /c run.bat "%1" >> "%2" 2>> "%3"
Use getOutputStream()
on the process, instead of using System.out.println()
. Sometimes the semantics change between Java implementations.
This seems to be a bugfix actually - the newer implementation makes sense.
This is the simplest method i found on http://tamanmohamed.blogspot.in/2012/06/jdk7-processbuilder-and-how-redirecting.html
File output = new File("C:/PBExample/ProcessLog.txt");
ProcessBuilder pb = new ProcessBuilder("cmd");
pb.redirectOutput(output);