【发布时间】:2013-09-24 10:38:10
【问题描述】:
这基本上就是我想要做的:我创建了一个模拟命令行的Process。像这样:
private Process getProcess() {
ProcessBuilder builder = new ProcessBuilder("C:/Windows/System32/cmd.exe");
Process p = null;
try {
p = builder.start();
} catch (IOException e1) {
e1.printStackTrace();
}
return p;
}
现在我可以用命令“喂”这个过程了:
BufferedWriter p_stdin = new BufferedWriter(
new OutputStreamWriter(process.getOutputStream()));
try {
p_stdin.write("dir"); // Just a sample command
p_stdin.newLine();
p_stdin.flush();
} catch (IOException e) {
e.printStackTrace();
return "Failed to run " + fileName;
}
我现在想做的是等待命令,即我的进程的子进程,完成。我怎样才能做到这一点?我知道我可以使用waitFor() 方法等待进程,但是子进程呢??
【问题讨论】:
-
my answer to a similar question 有帮助吗? (它使用
/bin/bash,但您应该可以将其替换为cmd.exe。)
标签: java shell command-line cmd