Reputation: 1953
I have a Windows runas
command that works perfectly when typed at the command line, but not when invoked from my Java program through Runtime.exec()
. The code looks almost exactly like this:
// 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();
I see the password prompt, Enter the password for DOMAIN\OtherUser:
, and then the process immediately terminates with an exit value of 1.
As an experiment, I moved the process.waitFor();
above the code that enters the password, expecting the process to hang at the password prompt. But, the process does not hang. It still terminates with 1! (And then the attempt to enter the password causes java.io.IOException: Stream Closed
.)
The same code will successfully run processes that don't require input (and don't go through runas
), and also successfully provides input to sudo --stdin
on Linux.
I've studied many similar questions with no luck.
The Windows in question is Windows 7 and the Java in question is Java 8.
Upvotes: 2
Views: 1499
Reputation: 1953
OK, looks like you can't do this:
How to fill password runas command in java
The reason being that the Windows runas
command does not read from stdin
, but directly from the console.
Upvotes: 1