1

I have one application that prints messages from Test.exe in console .My java program creates one process by executing this Test.exe. This application prints messages by reading from input-stream of that process.

The problem, that I am facing is,
I have two scenarios:
1) When I double click test.exe, messages("Printing : %d") are printing for every second.
2)But when I run my java application,whole messages are printing at last(not for every second) before terminating Test.exe.
If .exe has a very huge messages to print,then it will print those messages(I think whenever buffer becomes full)and flushing will be done.
But how can I print messages same as 1st case.

Help from anyone would be appreciated. :)

Here is the code for this Test.exe.

#include <stdio.h>
#include <windows.h>
void main(void)
 {
  int i=0;

  while (1)
     {  
    Sleep(500);
    printf("\nPrinting : %d",i);
    i++;
       if (i==10)
      //if(i==100)
        {
        return 0;
        }
     }
}

And my Java application is below:

public class MainClass {
public static void main(String[] args) {

    String str = "G:\\Charan\\Test\\Debug\\Test.exe";
    try {
        Process testProcess = Runtime.getRuntime().exec(str);

        InputStream inputStream = new BufferedInputStream(
                testProcess.getInputStream());
        int read = 0;
        byte[] bytes = new byte[1000];
        String text;
        while (read >= 0) {
            if (inputStream.available() > 0 ) {
                read = inputStream.read(bytes);
                if (read > 0) {
                    text = new String(bytes, 0, read);
                    System.out.println(text);
                }
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}
}

Is it possible in reverse order.If I input some text from console,Java should read and pass that String to .exe(or testProcess).How .exe scan something from Java program.

Could anyone help me..

1
  • 1
    Try adding in the c++ application fflush(stdout) after each printf Commented Mar 14, 2012 at 7:15

2 Answers 2

1

Given that you're trying to print stdout from that process line by line, I would created a BufferedReader object using the process' input stream and use the readLine() method on that. You can get a BufferedReader object using the following chain of constructors:

BufferedReader testProcessReader = new BufferedReader(new InputStreamReader(testProcess.getInputStream()));

And to read line by line:

String line;
while ((line = testProcessReader.readLine()) != null) {
    System.out.println(line);
}

The assumption here is that Test.exe is flushing its output, which is required by any read from the Java side. You can flush the output from C by calling fflush(stdout) after every call to printf().

If you don't flush, the data only lives in a buffer. When considering performance, it's a trade-off, how often you want the data to be written vs. how many writes / flush operations you want to save. If performance is critical, you can consider looking into a more efficient inter-process communication mechanism to pass data between the processes instead of stdout. Since you are on Windows, the first step might be to take a look at the Microsoft IPC help page.

Sign up to request clarification or add additional context in comments.

5 Comments

No change even after shifted to BufferedReader..Anyway, Thank you so much...I will check with flush()
My .exe prints messages very frequently.So each time flushing may lead to performance issue.Is there any alternative for this.?
If you don't flush, the data only lives in a buffer. It's a trade-off, how often do you want the data to be written vs. how many writes / flush operations you want to save. You can consider looking into a more efficient inter-process communication mechanism to pass data between the processes instead of stdout, however that solution will be platform dependent.
>> efficient inter-process communication mechanism to pass data between the processes instead of stdout. Could you please explain in detail.
Assuming you're on Windows, take a look at the Microsoft IPC help page. Just note that any other IPC mechanism will be a lot more involved than what you are currently doing.
0

Seems to have something to do with not flushing. I guess it's on both sides - The C library you use seems to only automatically flush output when writing to a terminal. Flush manually after calling printf.

On the Java side, try reading from a non-buffered stream.

2 Comments

Sorry for interrupting again.. There will be performance issue in our application as I flush each time.As I realized that our .exe prints messages very frequently.So each time flushing may lead to performance issue.Is there any alternative for this.?
output will only be seen by the java application after flushing. A compromise would be not to flush after every printf. It all depends on how your software is supposed to work. Maybe you will have to consider other alternatives for inter process communication as already pointed out in the comment to the other answer.

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.