Java 在Windows中输入并从控制台获取返回消息,如命令提示符
在Windows操作系统的命令提示符(cmd)中:Java 在Windows中输入并从控制台获取返回消息,如命令提示符,java,command-line,Java,Command Line,在Windows操作系统的命令提示符(cmd)中: C:\>execution.exe --login user Enter password: Success 工作流程是输入命令->系统返回消息“输入密码:”->输入密码->系统返回消息“成功”。 如何在我的代码中做到这一点? 这是我的,但不行 Process p = Runtime.getRuntime().exec("execution.exe --login user"); BufferedWriter writer = new
C:\>execution.exe --login user
Enter password:
Success
工作流程是输入命令->系统返回消息“输入密码:”->输入密码->系统返回消息“成功”。
如何在我的代码中做到这一点?
这是我的,但不行
Process p = Runtime.getRuntime().exec("execution.exe --login user");
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(p.getOutputStream()));
writer.write("mypassword\n");
writer.flush();
writer.close();
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("System returns message:\n");
String s = null;
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
// read any errors from the attempted command
System.out.println("System returns error message:\n");
while ((s = stdError.readLine()) != null) {
System.out.println(s);
}
您得到了什么错误/输出?没有。我已调试,程序无法通过此“s=stdInput.readLine()”。控制台的所有输出为:“系统返回消息:”