1

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.

Orgrim
  • 357
  • 1
  • 5
  • 13
  • 1
    Try some fflush(stdout) in the C program? The output will probably not be line buffered if it is not sent to a terminal. – Thomas Padron-McCarthy Jun 29 '12 at 18:15
  • Tried that, didn't work. Could it have something to do with encoding? – Orgrim Jun 29 '12 at 18:32
  • What happens if you call br.ready()? When it returns true, there should be data for reading. – Nick Jun 29 '12 at 18:39
  • br.ready() returns false even when there should be data to read (I tried using the same command sequence from a console and it works correctly). Could it be caused from the fact that the OutputStream to stdin remains open? – Orgrim Jun 29 '12 at 19:35
  • Yes it could. If you have no (more) output to deliver to the program, close it. – user207421 Jun 30 '12 at 02:33
  • Are you sure you are starting the right C program? I tried to run this, wrote a small C program that I named "ctest", and (stupidly) called it from Java as "ctest" and not with a full path or "./ctest". This started a completely different program. – Thomas Padron-McCarthy Jun 30 '12 at 08:23
  • 1
    I solved it by following this answer: http://stackoverflow.com/questions/3643939/java-process-with-input-output-stream I add a string to the end of the output and stop reading when I find it. – Orgrim Jun 30 '12 at 08:24
  • The usual solution is to do the reading in a separate thread, and call process.waitFor() in this thread. – user207421 Jul 01 '12 at 01:30

1 Answers1

0

When you call readLine() the BufferedReader will block until there is a new line character '\n' in the output. Try calling the C program from a terminal / command line see if the output really terminates in a new line.

Alternatively you could just read(1) one char at a time (no need for a BufferedReader then) and print it immediately.

Encoding does not affect the '\n' character.

mercutio
  • 1,065
  • 7
  • 18
  • Yes, I append a '\n' to every string the C program outputs, and from the terminal it works correctly. I don't think read() would work either, since as I stated above ready() returns false. – Orgrim Jun 29 '12 at 19:44