I have a (Windows) command-line application that, when launched, prompts you to enter a password and then prints some text. Unfortunately, I do not own the source to the application and the application does not take any arguments when you launch it (i.e., cannot pass the password in when you start the application). I need to programmatically launch the application in Java and send a password to it and then read the response. While I have had success launching other programs (that just have output), I cannot seem to capture any output from this application. Here is my Java code:
import java.io.IOException;
import java.io.InputStream;
import java.lang.ProcessBuilder.Redirect;
public class RunCommand {
public static void main(String[] args) throws Exception {
new RunCommand().go();
}
void go() throws Exception {
ProcessBuilder pb = new ProcessBuilder("executable.exe");
pb.redirectErrorStream(true); // tried many combinations of these redirects and none seemed to help
pb.redirectInput(Redirect.INHERIT);
pb.redirectOutput(Redirect.INHERIT);
pb.redirectError(Redirect.INHERIT);
Process process = pb.start();
final Thread reader = new Thread(new Runnable() {
@Override
public void run() {
try {
final InputStream is = process.getInputStream();
int c;
while ((c = is.read()) != -1) {
// never gets here because c is always = -1
System.out.println((char) c);
}
} catch (IOException e) {
e.printStackTrace();
}
}
});
reader.start();
boolean cont = true;
while (cont) {
// force this to continue so we can try and get something from the input stream
}
process.destroyForcibly();
}
}