【发布时间】:2011-11-08 14:02:31
【问题描述】:
我需要在命令行执行一个命令,并且在命令执行后需要提供一系列是或否的答案。 例如:
>./somecommand(返回)
你确定吗? [是/否]:是(返回)
你真的确定吗? [是/否]:是(返回)
最后的机会。 [是/否]:否(返回)
好的。
我正在尝试使用以下代码 sn-p 来完成这项任务。
try {
// Execute command
String command = "somecommand";
Process child = Runtime.getRuntime().exec(command);
// Get output stream to write from it
OutputStream out = child.getOutputStream();
out.write("yes".getBytes());
out.flush();
out = child.getOutputStream();
out.write( "yes".getBytes() );
out.flush();
out = child.getOutputStream();
out.write( "no".getBytes() );
out.close();
} catch (IOException e) {
}
如您所见,我尝试使用 'out = child.getOutputStream()' 三次,每次写入后刷新 'out'。但这似乎不起作用。 知道如何做到这一点吗?
【问题讨论】:
-
somecommand是否有--assume-yes或类似选项? -
否 .. 我需要捕获流程输出的问题,并在此基础上提供问题的答案。
标签: java command-line input exec runtime.exec