2

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();
    }

}
4
  • Do you get any errors of the errors stream ? minimal reproducible example could be helpful. In this case it should include a simulation of the program you are trying to listen to. Commented Jan 20, 2018 at 9:58
  • No, no errors. Also, when I run a vbscript script and execute the program I do get the standard output so I know the application writes to stdout. Commented Jan 21, 2018 at 0:16
  • I suppose I should add that I am running the code from within Eclipse, in case that matters. Commented Jan 21, 2018 at 0:17
  • See stackoverflow.com/questions/19238788/… Commented Jan 21, 2018 at 6:06

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.