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

}
Hmmmmm
  • 778
  • 9
  • 20
  • Do you get any errors of the errors stream ? [mcve] could be helpful. In this case it should include a simulation of the program you are trying to listen to. – c0der Jan 20 '18 at 09: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. – Hmmmmm Jan 21 '18 at 00:16
  • I suppose I should add that I am running the code from within Eclipse, in case that matters. – Hmmmmm Jan 21 '18 at 00:17
  • See https://stackoverflow.com/questions/19238788/get-output-of-running-java-program-from-another – c0der Jan 21 '18 at 06:06

0 Answers0