[英]Running command from java produces empty output
I need to run the below comand using java but it is running fine in terminal as 我需要使用Java运行以下命令,但在终端中运行正常
svn list http://192.168.0.19/svn/cc/Branch/Jobs/tt/jobs/ --username prasadh --password prasadh2k > output.txt svn列表http://192.168.0.19/svn/cc/Branch/Jobs/tt/jobs/ --username prasadh --password prasadh2k> output.txt
But when running the same via process builder it is returning empty result. 但是,当通过流程生成器运行相同的内容时,它将返回空结果。
My code: 我的代码:
ProcessBuilder pb = new ProcessBuilder("cmd", "C:\\Users\\dev112\\output", "svn", "list", "http://192.168.0.19/svn/cadgraf/Branch/Jobs/T0003SATHYABAMAT/Completedjobs", "--username", "prasadh", "--password", "prasadh2k", ">", "output.txt");
pb.redirectErrorStream(true);
try {
Process p = pb.start();
new Thread(new InputConsumerforImageMagick.InputConsumer(p.getInputStream())).start();
try {
System.err.println("Exited with: " + p.getErrorStream());
} catch (Exception ex) {
Logger.getLogger(AddImage.class.getName()).log(Level.SEVERE, null, ex);
}
} catch (IOException ex) {
Logger.getLogger(AddImage.class.getName()).log(Level.SEVERE, null, ex);
}
Don't go through cmd
. 不要通过
cmd
。 Just run the command directly: 只需直接运行命令:
final Path cwd = Paths.get("c:\\Users\\dev112\\output");
Files.createDirectories(cwd);
final Path outfile = cwd.resolve("output.txt");
final ProcessBuilder pb = new ProcessBuilder("svn", "list",
"http://192.168.0.19/svn/cadgraf/Branch/Jobs/T0003SATHYABAMAT/Completedjobs",
"--username", "prasadh", "--password", "prasadh2k");
pb.directory(cwd.toFile());
pb.redirectOutput(outfile.toFile());
final int retcode = pb.start().waitFor();
What is more, why do you get the process' standard output if you output to a file? 此外,如果输出到文件,为什么还要获得流程的标准输出? Do one or the other, not both.
做一个或另一个,不要同时做。 If you output to a file then read the contents of that file after the command is executed.
如果输出到文件,则在执行命令后读取该文件的内容。
The sample above outputs to a file; 上面的示例输出到文件; just open a stream to that file afterwards using
Files.newInputStream(outfile)
(well, that is, if retcode
is 0; if it isn't, your command has ended with an error; which also means that you should redirect stderr somewhere, too) 只需稍后使用
Files.newInputStream(outfile)
打开该文件的流(好吧,也就是说,如果retcode
为0;如果不是,则您的命令以错误结尾;这也意味着您应该将stderr重定向到某个地方,太)
I/O redirection doesn't work well with ProcessBuilder
. I / O重定向在
ProcessBuilder
中不能很好地工作。 You should either call cmd.exe
with 您应该使用以下命令调用
cmd.exe
new ProcessBuilder("cmd", "/c", "svn ... > output.txt");
(ie you have to call cmd
with exactly two arguments) or you must redirect yourself, that is you need to start a background thread which reads stdout from the process and writes it to output.txt
. (即,您必须使用两个参数来调用
cmd
),或者您必须重定向自己,也就是说,您需要启动一个后台线程,该线程从进程读取stdout并将其写入output.txt
。 In that case, you should use: 在这种情况下,您应该使用:
new ProcessBuilder("svn", "list", ...);
The former is brittle when you have spaces in arguments. 当参数中有空格时,前者很脆弱。 So I suggest the latter even though the Java code is much more complex.
因此,即使Java代码复杂得多,我还是建议后者。
You should also have a look at Commons Exec which makes it much easier to deal with external processes. 您还应该看看Commons Exec ,它使处理外部流程变得更加容易。
Or with Java 7, you can use pb.redirectOutput();
或在Java 7中,可以使用
pb.redirectOutput();
This works for me: 这对我有用:
String command = "svn list http://192.168.0.19/svn/cc/Branch/Jobs/tt/jobs/ --username prasadh --password prasadh2k";
ProcessBuilder processBuilder = new ProcessBuilder(command.split());
processBuilder.redirectOutput(new File("C:/Users/dev112/output/", "output.txt"));
processBuilder.start();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.