【发布时间】:2018-06-15 20:34:50
【问题描述】:
我有一个 Windows runas 命令,在命令行键入时可以完美运行,但在通过Runtime.exec() 从我的 Java 程序调用时不能。代码看起来几乎完全一样:
// Create the process.
Process process = Runtime.getRuntime().exec(
String.format(
"runas /noprofile /user:DOMAIN\\OtherUser \"%s\",
command));
// Enter the password.
try (OutputStream outputStream = process.getOutputStream();
PrintWriter printWriter = new PrintWriter(outputStream)) {
printWriter.printf("%s\n", password);
printWriter.flush();
} catch (IOException ex) {
ex.printStackTrace();
}
// Wait for the process to terminate.
process.waitFor();
我看到密码提示Enter the password for DOMAIN\OtherUser:,然后进程立即以退出值 1 终止。
作为一个实验,我将process.waitFor(); 移到了输入密码的代码上方,希望进程在密码提示符处挂起。但是,该过程不会挂起。它仍然以 1 结束! (然后尝试输入密码会导致java.io.IOException: Stream Closed。)
相同的代码将成功运行不需要输入的进程(并且不通过runas),并在 Linux 上成功地向sudo --stdin 提供输入。
我研究了许多类似的问题,但没有运气。
有问题的 Windows 是 Windows 7,有问题的 Java 是 Java 8。
【问题讨论】:
-
哦,亲爱的。不可能?:stackoverflow.com/questions/19789115/…