Java 使用exec向命令发送多个输入
我需要在命令行执行一个命令,在执行命令后需要提供一系列是或否的答案。 例如:Java 使用exec向命令发送多个输入,java,command-line,input,exec,runtime.exec,Java,Command Line,Input,Exec,Runtime.exec,我需要在命令行执行一个命令,在执行命令后需要提供一系列是或否的答案。 例如: >/somecommand(返回) 你确定吗?[是/否]:是(返回) 你真的确定吗?[是/否]:是(返回) 最后的机会。[是/否]:否(返回) 嗯 我正试图使用以下代码片段来完成此任务 try { // Execute command String command = "somecommand"; Process child = Runtime.getRuntime().exec(command)
>/somecommand(返回) 你确定吗?[是/否]:是(返回) 你真的确定吗?[是/否]:是(返回) 最后的机会。[是/否]:否(返回) 嗯
我正试图使用以下代码片段来完成此任务
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'。但这似乎不起作用。
知道怎么做吗?在while循环中尝试java
readLine()
命令。
给它一个终止条件,如
Yes
或No
检查以下帖子:somecommand
是否有——假设是或类似选项?否。。我需要捕获流程输出的问题,并在此基础上提供问题的答案。