Problem getting a C application stdout using Process and InputStream.

Hi,

I'm having a very rare problem.

In Ubuntu Linux, I have a simple Java application that executes others applications, gets their stout and prints them:

Process p=Runtime.getRuntime().exec ("./application");

InputStream is = p.getInputStream();

BufferedReader br =new BufferedReader (new InputStreamReader (is));

String line = br.readLine();

while (line!=null){

System.out.println (line);

line = br.readLine();

}

It works well using several applications (the are written in C).

However I have one that writes each second a line in the stdout (using the C's printf(" ") or fprintf(stdout," "), and this code does not work well, because it executes the application but it waits for its end for printing all.

I mean, if the execution have a duration of 10 seconds, the java program waits 10 seconds and after it prints all lines in a quckly burst.

However, if I execute that application in the shell, it writes each second a line and it works fine.

I dont understand why my Java program is not capable to get each second the current line and prints it in this C application.

Any idea? Thanks in advance.

[1419 byte] By [JAX82a] at [2007-11-27 9:13:23]
# 1

import java.io.*;

public class a {

public static void main(String arg[])throws Exception {

Process p=Runtime.getRuntime().exec ("./application");

InputStream is = p.getInputStream();

BufferedReader br = new BufferedReader (new InputStreamReader (is));

String line = null;

while ((line=br.readLine()) !=null){

System.out.println (new java.sql.Timestamp(System.currentTimeMillis()) + " " + line);

}

}

}

$ uname -a

Linux giskard 2.6.9-42.0.3.ELsmp #1 SMP Mon Sep 25 17:28:02 EDT 2006 i686 i686 i386 GNU/Linux

$ cat application

while(true)

do

date

sleep 1

done

[ebank2@giskard ~/ivan]$ java a

2007-06-29 11:41:10.707 Fri Jun 29 11:41:10 CEST 2007

2007-06-29 11:41:11.72 Fri Jun 29 11:41:11 CEST 2007

2007-06-29 11:41:12.734 Fri Jun 29 11:41:12 CEST 2007

2007-06-29 11:41:13.744 Fri Jun 29 11:41:13 CEST 2007

2007-06-29 11:41:14.754 Fri Jun 29 11:41:14 CEST 2007

2007-06-29 11:41:15.766 Fri Jun 29 11:41:15 CEST 2007

2007-06-29 11:41:16.777 Fri Jun 29 11:41:16 CEST 2007

2007-06-29 11:41:17.789 Fri Jun 29 11:41:17 CEST 2007

2007-06-29 11:41:18.8 Fri Jun 29 11:41:18 CEST 2007

2007-06-29 11:41:19.81 Fri Jun 29 11:41:19 CEST 2007

2007-06-29 11:41:20.821 Fri Jun 29 11:41:20 CEST 2007

2007-06-29 11:41:21.836 Fri Jun 29 11:41:21 CEST 2007

BIJ001a at 2007-7-12 22:00:45 > top of Java-index,Java Essentials,Java Programming...
# 2
> I'm having a very rare problem.No you're not. C programs that use stdio to write to stdout automatically buffer it unless stdout is a terminal. The stdio library does this.Why is that a problem?
ejpa at 2007-7-12 22:00:45 > top of Java-index,Java Essentials,Java Programming...
# 3
Hi,Thanks for your replies. I have solved the problem using fflush(stdio) in the C application each time.Regards.
JAX82a at 2007-7-12 22:00:45 > top of Java-index,Java Essentials,Java Programming...