0

I am trying to communicate between a process in my java program and an external python script that is called by the java program. The problem arises that my java program only receives the output of the python program after it has terminated. This means I cannot have a duplex "conversation" between the processes, which is my goal. Here is my python code:

for i in range(3):
    print("hello"); #send hello to java
    line = sys.stdin.readline(); #read read response from java

and my java code:

    String line;
    Process external = Runtime.getRuntime().exec(execStr);
    BufferedReader in = new BufferedReader(new InputStreamReader(external.getInputStream()));
    PrintStream out = new PrintStream(external.getOutputStream(),true);


    while ((line = in.readLine)) != null){//while python has not yet terminated
        if (line.length > 0){ // if not a bogus line
            handleLine();
            sendResponse();
        }

    }

The Java then hangs on the next readLine, because it never receives a next output from the python.

Thanks for your help :)

GregarityNow
  • 707
  • 1
  • 11
  • 25
  • That is not your Java code: it misses several semicolons. – Arndt Jonasson Apr 19 '18 at 10:19
  • `in.readline()`, rather? I think the code you have pasted is not the code you are running. – Arndt Jonasson Apr 19 '18 at 12:18
  • perhaps this was poor practice, but i made a sort of simplified pseudo-version to illustrate the important parts and you're right I missed the capital L in readLine() ... sorry about that, i should've been more thorough – GregarityNow Apr 19 '18 at 12:30

1 Answers1

0

There may well be a better answer to this but I never "flushed" the output from python (i figured that would be done automatically) but by adding a simple sys.stdout.flush() after the print line, it solved my problem.

GregarityNow
  • 707
  • 1
  • 11
  • 25