Java:从Java运行批处理脚本时检测用户提示
我需要从Java执行一个批处理脚本,它执行以下操作 1) 一旦启动,它将执行一项长时间(最多几秒钟)的任务 2) 此后,它会显示提示“密码:” 3) 然后,用户输入密码并按Enter键 4) 然后,脚本完成其工作 我知道如何从Java启动脚本,我知道如何用Java读取批处理脚本的输出,但我不知道如何等待出现密码提示(我如何知道批处理脚本正在等待密码输入) 所以,我的问题是:如何知道批处理脚本何时打印了提示 目前,我有以下代码:Java:从Java运行批处理脚本时检测用户提示,java,batch-file,Java,Batch File,我需要从Java执行一个批处理脚本,它执行以下操作 1) 一旦启动,它将执行一项长时间(最多几秒钟)的任务 2) 此后,它会显示提示“密码:” 3) 然后,用户输入密码并按Enter键 4) 然后,脚本完成其工作 我知道如何从Java启动脚本,我知道如何用Java读取批处理脚本的输出,但我不知道如何等待出现密码提示(我如何知道批处理脚本正在等待密码输入) 所以,我的问题是:如何知道批处理脚本何时打印了提示 目前,我有以下代码: final Runtime runtime = Runtime.ge
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);
}
这应该可以做到:
public static void main(final String... args) throws IOException, InterruptedException {
final Runtime runtime = Runtime.getRuntime();
final String command = "..."; // cmd.exe
final Process proc = runtime.exec(command, null, new File("."));
final BufferedReader input = new BufferedReader(new InputStreamReader(proc.getInputStream()));
StringBuilder sb = new StringBuilder();
char[] cbuf = new char[100];
while (input.read(cbuf) != -1) {
sb.append(cbuf);
if (sb.toString().contains("Password:")) {
break;
}
Thread.sleep(1000);
}
System.out.println(sb);
}
这一条似乎有效:
@Override
public void run() throws IOException, InterruptedException {
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 batchFileOutput = "";
while (input.ready()) {
char character = (char) input.read();
batchFileOutput = batchFileOutput + character;
}
// Batch script has printed the banner
// Wait for the password prompt
while (!input.ready()) {
Thread.sleep(1000);
}
// The password prompt isn't terminated by a newline - that's why we can't use readLine.
// Instead, we need to read the stuff character by character.
batchFileOutput = "";
while (input.ready() && (!batchFileOutput.endsWith("Password: "))) {
char character = (char) input.read();
batchFileOutput = batchFileOutput + character;
}
// When we are here, the prompt has been printed
// It's time to enter the password
if (batchFileOutput.endsWith("Password: ")) {
final BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(proc.getOutputStream()));
writer.write(this.password);
// Simulate pressing of the Enter key
writer.newLine();
// Flush the stream, otherwise it doesn't work
writer.flush();
}
// Now print out the output of the batch script AFTER we have provided it with a password
String line;
while ((line = input.readLine()) != null) {
LOGGER.debug("proc: " + line);
}
// Print out the stuff on stderr, if the batch script has written something into it
final BufferedReader error = new BufferedReader(new InputStreamReader(
proc.getErrorStream()));
String errorLine = null;
while ((errorLine = error.readLine()) != null) {
LOGGER.debug("proc2: " + errorLine);
}
// Wait until the program has completed
final int result = proc.waitFor();
// Log the result
LOGGER.debug("result: " + result);
}
您确定它是getOutputStream()吗?目前,我可以使用input.readLine()查看批处理脚本的部分输出(请参阅代码段中的日志记录语句)。但不是全部——我无法用这种方式检测到提示。我的第一个回答是胡说八道。javadoc解释了输出和输入流:。感谢您的代码。不幸的是,密码提示没有以换行符终止。这就是为什么readline在这种情况下不起作用。我更新了代码以反映您的编辑(没有readline()。你可以读入一个字符[],而不是读入单个字符,但这只是一个小的性能调整。对我来说效果很好。