【发布时间】:2017-04-13 00:20:34
【问题描述】:
我有一个带有一些命令的 shell 脚本,其中一个命令期望在运行时输入一些数据。我正在使用 exec() 方法运行这个 shell 脚本。目前,如果要求输入,我正在手动输入数据。以下是我的代码,
Process p = Runtime.getRuntime().exec("myshellscript");
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
// read the output from the command
System.out.println("Here is the standard output of the command:\n");
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
// read any errors from the attempted command
System.out.println("Here is the standard error of the command (if any):\n");
while ((s = stdError.readLine()) != null) {
System.out.println(s);
}
System.exit(0);
我想要做的是每当控制台期望的数据被输入时,它应该由java程序输入,然后继续我的进一步的java程序。
【问题讨论】:
标签: java shell console command processbuilder