我正在使用运行时执行一个命令,该命令接受两次密码(例如:输入密码,验证密码)。我正在使用以下代码。我面临的问题是程序挂起,因为它正在等待验证密码。第一个密码被正确传递(我通过从我的命令和java代码中删除验证密码进行验证,它可以工作),验证密码没有传递给命令并且命令无限期地等待验证密码。如果有人有任何建议,请告诉我。
try
{
runtime = Runtime.getRuntime();
process = runtime.exec("<<my command>>"");
String inLine = "";
String errLine = "";
StringBuffer inBuffer = new StringBuffer();
StringBuffer errBuffer = new StringBuffer();
PrintWriter pw = new PrintWriter(process.getOutputStream());
pw.print("<<password>>"+"\n");
pw.print("<<verify password>>"+"\n");
pw.flush();
BufferedReader stdin = new BufferedReader(new InputStreamReader(
process.getInputStream()));
BufferedReader stderr = new BufferedReader(new InputStreamReader(
process.getErrorStream()));
while ((inLine = stdin.readLine()) != null) {
inBuffer = inBuffer.append(inLine + "\n");
}
stdin.close();
System.out.println("Output messages of cmd " + inBuffer.toString());
while ((errLine = stderr.readLine()) != null) {
errBuffer = errBuffer.append(errLine + "\n");
}
stderr.close();
System.out.println("Error messages of cmd " + errBuffer.toString());
process.waitFor();
int exitCode = process.exitValue();
System.out.println("cmd " + " exited with code " + exitCode);
}