0

I created a sub Process and I'm trying to read it output and print it. The process is a very simple python program that prints "Tick" every second and after some time exits.

The java code is:

[..]
    try
    {
        ProcessBuilder pb = new ProcessBuilder(command);
        pb.redirectErrorStream(true);
        Process p = pb.start();

        InputStreamReader in = new InputStreamReader(p.getInputStream());
        BufferedReader br = new BufferedReader(in);

        String line;

        while ((line = br.readLine()) != null)
        {
            System.out.println(line);
            System.out.flush();
        }
    }
    catch (Exception ex)
    {
        System.out.println(ex.getMessage());
        System.exit(1);
    }
[..]

I only see the output on the console once the python program has exited. How can I read and print at the same time? Thanks

pistacchio
  • 53,670
  • 101
  • 270
  • 404
  • 1
    Your python program probably needs to [flush](http://stackoverflow.com/questions/230751/how-to-flush-output-of-python-print) it's standard output. – Piotr Praszmo Oct 23 '11 at 15:11
  • oh, you where right thank you. if you submit as an answer, i'd accept it. thanks. – pistacchio Oct 23 '11 at 15:18

0 Answers0