0

First, I saw a few Q's about this issue in the site, but didn't see any answer that solve my problem.

I have a program written in Java and it calls a cmd program written in C++. (this is an assumption since I don't have the actual source) I know the expected I/O of the C++ program, in the cmd it is two lines of output and then it waits for string input. I know that the first output line of the program is through error stream, and I receive it properly (this is expected), but I don't get the second line in error or input stream. I tried to write to the program right after the first line ( the error line) and didn't got stuck, but there was no response. I tried using 3 different threads, for each stream, but again, nothing was received in input/error stream after the first line, and the program didn't respond to writing through output stream.

My initializers are:

Process p = Runtime.getRuntime().exec("c:\\my_prog.exe");
BufferedReader err = new BufferedReader(new InputStreamReader(p.getErrorStream()));
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
BufferedWriter output = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));

Is it possible at all or maybe it depends on the C++ program?

Thanks, Binyamin

MByD
  • 135,866
  • 28
  • 264
  • 277
  • If the C++ program uses streams for input and output it will work. – Peter Lawrey Dec 26 '10 at 17:39
  • What does your program output when you let it print the strings it receives? Both lines combined or just the first one? Is the expected input for the console program after some prompt or in an empty line? – thejh Dec 26 '10 at 17:54
  • Only the first line. I debugged it a bit with IDA, and the first line is going out through error stream. the second line seems to go out in standard output, though I'm not sure, because my IDA skills suck. The 1st and 2nd line are results of different checks of the program. – MByD Dec 26 '10 at 18:01
  • Does the output of the second line end with a newline? – thejh Dec 26 '10 at 19:05
  • @thejh: At the beginning no, but I added a new line character in the binary file and when I ran it in cmd it worked perfectly and started a new line at the end of the second line. – MByD Dec 26 '10 at 20:08

3 Answers3

0

If you want to call native applications like C and C++ from Java, you need to use JNI.

Rendicahya
  • 4,205
  • 7
  • 36
  • 56
  • 3
    Uhmmm... not for calling native **applications**, just for calling native **libraries**. – thejh Dec 26 '10 at 17:51
0

I would suggest to put the input in the program when it has started, it will propably use that as input when it wants it.

thejh
  • 44,854
  • 16
  • 96
  • 107
0

Here is how I execute any command line in Java. This command line may execute any program:

private String executionCommandLine(final String cmd) {

    StringBuilder returnContent = new StringBuilder();
    Process pr;
    try {
        Runtime rt = Runtime.getRuntime();
        pr = rt.exec(cmd);

        BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream()));

        String line = null;

        while ((line = input.readLine()) != null) {
            returnContent.append(line);
        }
        input.close();
        LOG.debug(returnContent.toString());

        // return the exit code
        pr.waitFor();
    } catch (IOException e) {
        LOG.error(e.getMessage());
        returnContent = new StringBuilder();
    } catch (InterruptedException e) {
        LOG.error(e.getMessage());
        returnContent = new StringBuilder();
    }

    return returnContent.toString();

}
Eric V
  • 1,150
  • 1
  • 9
  • 24