Java:从 Java 运行批处理脚本时检测用户提示
我需要从 Java 执行一个批处理脚本,该脚本执行以下操作
: 1) 一旦启动,它就会执行一项冗长(长达几秒)的任务。
2) 之后,显示提示“密码:”。
3) 然后,用户输入密码并按 Enter 键。
4) 然后,脚本完成其工作。
我知道如何从 Java 启动脚本,我知道如何读取 Java 中批处理脚本的输出,但我不知道如何等待密码提示出现(我如何知道批处理脚本正在等待密码输入)。
所以,我的问题是:如何知道批处理脚本何时打印了提示?
目前,我有以下代码:
final Runtime runtime = Runtime.getRuntime();
final String command = ... ;
final Process proc = runtime.exec(command, null, this.parentDirectory);
final BufferedReader input = new BufferedReader(new InputStreamReader(
proc.getInputStream()));
String line = null;
while ((line = input.readLine()) != null) {
LOGGER.debug("proc: " + line);
}
I need to execute from Java a batch script, which does following
1) Once it is started it performs a lengthy (up to several seconds) task.
2) Thereafter, it displays a prompt "Password:".
3) Then, the user types in the password and presses the Enter key.
4) Then, the script completes its job.
I know how to launch the script from Java, I know how to read output of the batch script in Java, but I don't know how to wait for the password prompt to appear (how I get to know that the batch script is awaiting the password entry).
So, my question is: How to get to know when the batch script has printed the prompt?
At the moment, I have following code:
final Runtime runtime = Runtime.getRuntime();
final String command = ... ;
final Process proc = runtime.exec(command, null, this.parentDirectory);
final BufferedReader input = new BufferedReader(new InputStreamReader(
proc.getInputStream()));
String line = null;
while ((line = input.readLine()) != null) {
LOGGER.debug("proc: " + line);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
那应该可以完成这项工作:
That should do the job:
这似乎有效:
This one seems to work: