I am quite aware that this might be more of a Java/Python problem but it's on the RasPi and I also don't know if Java or Python is the problem.

So here is the situation:

I have a python script which is run by a Java Program with Runtime exec.

The two programs communicate quite simply with each other if the java Programm sends "go" the python script does something, once its done it returns "done"

while true:
    #do something

Running this in command line works fine, it does just what its supposed to.

And the java program Opens input and output streams from the Process.

Process p = Runtime.getRuntime().exec("python test1.py "+number1+" "+number2);
BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
BufferedReader out = new BufferedReader(new OutputStreamReader(p.getOutputStream()));

So here is where it gets messy.

If I run this and check for the input stream to be ready, it won't be, it will never be ready so I can't read from it. If I remove the line raw_input('') the input stream in Java will be readable but I just can't communicate with the script anymore.

I am pretty sure it's something very little which I just don't see! Thanks for the help :)

1 Answer 1


When you run from Java, the python interpreter knows that it's not connected to an interactive terminal and shuts down the in/out that you're using. Try using the -i flag to python to force interactive mode.

To use that flag, I think you'll also have to use ProcessBuilder in Java rather than exec as you've done, but you might be able to make it work with exec too.

  • The -i flag changed nothing. I found out that, as soon as the python script is in a blocking call (like raw_input or time.sleep) I can't read the output anymore even the lines that were printed before that.
    – Clex
    Feb 23, 2019 at 10:16
  • I think you have multiple problems then. I still think you need the -i flag, but you may also need to flush your buffers on either or both sides when you write.
    – Brick
    Feb 23, 2019 at 14:50
  • Thank you! I flushed in Java but not stdout in python, doing that solved the issue!
    – Clex
    Feb 24, 2019 at 11:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.