java 如何将进程输出通过管道传输到 Windows 和 JDK 6u45 上的文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17089875/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I Pipe process output to a file on Windows and JDK 6u45
提问by Barak
I have the following windows batch file (run.bat):
我有以下 Windows 批处理文件(run.bat):
@echo off
echo hello batch file to sysout
And the following java code, which runs the batch files and redirects output to a file:
以及以下 java 代码,它运行批处理文件并将输出重定向到一个文件:
public static void main(String[] args) throws IOException {
System.out.println("Current java version is: " + System.getProperty("java.version"));
ProcessBuilder pb =
new ProcessBuilder("cmd.exe", "/c",
"run.bat"
,">>", "stdout.txt","2>>", "stderr.txt"
);
System.out.println("Command is: " + pb.command());
Process proc = pb.start();
InputStream in = proc.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line = null;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
int exitValue = proc.exitValue();
System.out.println("Exit value: " + exitValue);
}
On JDKs up to and including JDK6u43 I get the following output:
在包括 JDK6u43 在内的 JDK 上,我得到以下输出:
Current java version is: 1.6.0_29
Command is: [cmd.exe, /c, run.bat, >>, stdout.txt, 2>>, stderr.txt]
Exit value: 0
and the script output is written to the file. As of JDK 6u45 and 7, I get the following output:
并将脚本输出写入文件。从 JDK 6u45 和 7 开始,我得到以下输出:
Current java version is: 1.6.0_45
Command is: [cmd.exe, /c, run.bat, >>, stdout.txt, 2>>, stderr.txt]
hello batch file to sysout
Exit value: 0
And nothing is written to the output file.
并且没有任何内容写入输出文件。
This may or may not be related to the changes made in Runtime.exec() , described at: http://www.oracle.com/technetwork/java/javase/6u45-relnotes-1932876.html
这可能与 Runtime.exec() 中所做的更改有关,也可能无关,描述在:http: //www.oracle.com/technetwork/java/javase/6u45-relnotes-1932876.html
What is the correct way of starting a process on Windows with output redirected to files?
在 Windows 上启动进程并将输出重定向到文件的正确方法是什么?
Note: In a real world scenario, the command to execute may include parameters with spaces, as in:
注意:在实际场景中,要执行的命令可能包含带空格的参数,如下所示:
ProcessBuilder pb = new ProcessBuilder("cmd.exe", "/c",
"run.bat", "Some Input With Spaces",
">>", "stdout.txt","2>>", "stderr.txt");
采纳答案by David Rabinowitz
Several suggestions here:
这里有几个建议:
- Does the input with the spaces need to be treated as single String (with spaces),or id it in actual several inputs? If the first Option is the case I would suggest to quote it for the windows runtime:
- 带空格的输入是否需要被视为单个字符串(带空格),或者在实际的多个输入中进行 id 处理?如果第一个选项是这种情况,我建议为 Windows 运行时引用它:
ProcessBuilder pb = new ProcessBuilder("cmd.exe", "/c",
"run.bat", "\"Some Input With Spaces\"",
">>", "stdout.txt","2>>", "stderr.txt");
- Instead of redirecting the input to stdout.txt and stderr.txt using the shell, why not do it using Java using getOutputStream() and getErrorStream()? Here is an example using Guava's IO package. Of course you may want to have those in separate threads, you need proper exception handling, etc.
- 与其使用 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();
- Another option, why not create a
run.bat
wrapper that will make the redirections?
- 另一种选择,为什么不创建一个
run.bat
可以进行重定向的包装器?
@echo off
cmd.exe /c run.bat "%1" >> "%2" 2>> "%3"
回答by Sanjay Bhosale
This is the simplest method i found on http://tamanmohamed.blogspot.in/2012/06/jdk7-processbuilder-and-how-redirecting.html
这是我在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);
回答by TFuto
Use getOutputStream()
on the process, instead of using System.out.println()
. Sometimes the semantics change between Java implementations.
使用getOutputStream()
的过程,而不是使用System.out.println()
。有时,Java 实现之间的语义会发生变化。
This seems to be a bugfix actually - the newer implementation makes sense.
这实际上似乎是一个错误修正 - 较新的实现是有道理的。