1

I've got an external executable program which I want to run from within my java program. While it is running, I need a way to get what the .exe is outputting, and also a way to then send input back to the .exe. This is what I've conjured up so far:

public int run(){
    String cmdRun = "cmd C:\\Meister\\Student\\bin\\Student.exe";

    int returnCode = 1;
    try {
        Process p = Runtime.getRuntime().exec(cmdRun); // makes appropriate system call
        BufferedReader stdError = new BufferedReader(new
                                  InputStreamReader(p.getErrorStream()));
        BufferedReader stdInput = new BufferedReader(new
                                  InputStreamReader(p.getInputStream()));
        String s = null;

        // read any info from the attempted command
        while ((s = stdError.readLine()) != null){ returnCode = -1; break; }
        while ((s = stdInput.readLine()) != null){ System.out.println(s); }
        System.out.println("Finished with exit code " + p.waitFor());
    } catch (Exception e) {
        e.printStackTrace();
    }

    return returnCode;
}

How should I properly RUN (1) the program, GET (2) the program output, and then SEND (3) input to the program when necessary?

EDIT: btw, when I run the method seen above, I do not see any output even though the file is being executed.

Quinn McHugh
  • 1,577
  • 2
  • 17
  • 23

1 Answers1

0

Try creating a file that will hold the input you would like the .exe to receive and create another file to hold the output of the .exe so it can be read by the java program.

if this didn't help here are some links that will hopefully will :

all about the process builder: http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/ProcessBuilder.html

I also found this while searching a little while :

java runtime.getruntime() getting output from executing a command line program

Community
  • 1
  • 1
  • This doesn't really answer my question. I'm looking for a way to capture the output from the `Process` and a way to send messages to the program. – Quinn McHugh Apr 18 '14 at 23:21