java正在ProcessBuilder中等待输出命令完成(“sh”)
如何等待“cmd1”退出,然后在不使用shell脚本的情况下运行“cmd2”java正在ProcessBuilder中等待输出命令完成(“sh”),java,Java,如何等待“cmd1”退出,然后在不使用shell脚本的情况下运行“cmd2” void exec(CharSequence ch) throws IOException { String string = ch.toString(); out.write(string); out.newLine(); out.flush(); //Fix here. } ProcessBuilder bui
void exec(CharSequence ch) throws IOException {
String string = ch.toString();
out.write(string);
out.newLine();
out.flush();
//Fix here.
}
ProcessBuilder builder = new ProcessBuilder("sh");
builder.redirectErrorStream(true);
process = builder.start();
out = new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));
exec("sleep 5"); // cmd1
exec("ls -al"); // cmd2
过程
有一种方法:
Process.waitFor()
如果需要,使当前线程等待,直到此进程
对象表示的进程终止。对不起,waitFor()是等待java调用的子进程,而不是等待子进程调用的进程在outputstream中完成。