Trouble getting standard output in Java from a C program with getchar()-Collection of common programming errors
I’m trying to call a C program from Java and capture the standard output. Here is my code:
try {
ProcessBuilder pb = new ProcessBuilder("helloworld.exe");
pb.redirectErrorStream(true); // Merge std out and std err into same stream
program = pb.start(); // Start program
BufferedReader input = new BufferedReader(new InputStreamReader(program.getInputStream()));
line = input.readLine();
while (line != null) {
System.out.println(line);
line = input.readLine();
}
} catch (IOException e) {
e.printStackTrace();
}
Here is a sample c program:
int main(){
printf("Hello world\n");
}
This works fine when the program I’m executing (helloworld in this case) does not have a getchar()
in it. However, if I add a getchar()
right after the printf, I never get anything off the input stream. Any ideas why?
Thanks