I'm writing a program in Java that sends commands to a C program and reads its output, but I see that it hangs when I try to actually read the output. The code is:
import java.io.*;
public class EsecutoreC {
private String prg;
OutputStream stdin=null;
InputStream stderr=null;
InputStream stdout=null;
BufferedReader br;
Process pr;
public EsecutoreC(String p){
prg=p;
try {
pr=Runtime.getRuntime().exec(prg);
stdin=pr.getOutputStream();
stderr=pr.getErrorStream();
stdout=pr.getInputStream();
br=new BufferedReader(new InputStreamReader(stdout));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void sendInput(String line){
line=line+"\n";
try {
stdin.write(line.getBytes());
stdin.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public BufferedReader getOutput(){
return br;
}
}
When I need to read the program's output, I just use getOutput to get the BufferedReader and then I call readLine(). I see that the program hangs since I put a println right after the call to readLine and nothing gets printed. I also add a \n after each line of output in the C program. Also, is there a way to show the window of the program called through Runtime.getRuntime().exec()? so that I could check if there is any other problem when I send a string to it or when I read from its output.