java - 使用 exec 将多个输入发送到命令

标签 java command-line input exec runtime.exec

我需要在命令行执行一个命令,并且在执行该命令后需要提供一系列是或否答案。 例如:

<小时/>

>./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() 命令。 给它一个终止条件,例如 YesNo

关于java - 使用 exec 将多个输入发送到命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7273832/

相关文章:

java - @RolesAllowed 无法通过 Jersey 解决

Java - 字母替换不起作用

java - @RequestMapping(value = "v1/firewall/policy/{zoneId:.*\\D+.*}") 是做什么的?

windows - 在 Dos 批处理文件中运行顽固的可执行文件

python - 询问用户输入,直到他们给出有效的响应

java - 用于 hibernate 实体映射的 Json 到 Java 对象的转换

node.js - 打开终端时出错

shell - 如何接受以反斜杠结尾的命令行参数

linux - 决定如何使用 set 和 shift 的更清洁的解决方案

javascript - 防止箭头键更改数字输入中的值