0

Unable to communicate from a python script with java program. I have a java program that reads from standard input. Logic is:

public static void main(String[] args) {
  ...
  BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  String cmd;
  boolean salir = faslse

  while (!salir) {
    cmd = in.readLine();
    JOptionPane.showMessageDialog(null, "run: " + cmd);
    //execute cmd
    ... 
    System.out.println(result);
    System.out.flush();
  }

}

I run the program through the console console

java -cp MyProgram.jar package.MyMainClass

And execute commands and get results, and shows the command executed in a dialogue (JOptionPane.showMessageDialog(null, "run: " + cmd); )

I need to call the program from python. Right now I'm trying with this:

#!/usr/bin/python
import subprocess

p = subprocess.Popen("java -cp MyProgram.jar package.MyMainClass", shell=True, stdout=subprocess.PIPE , stdin=subprocess.PIPE)
print '1- create ok'
p.stdin.write('comand parameter1 parameter2')
print '2- writeComand ok'
p.stdin.flush()
print '3- flush ok'
result = p.stdout.readline()  # this line spoils the script
print '4- readline ok'
print result
p.stdin.close()
p.stdout.close()
print 'end'

And the output is

1- create ok
2- writeComand ok
3- flush ok

And does not show the dialog.

however if I run:

#!/usr/bin/python
import subprocess

p = subprocess.Popen("java -cp MyProgram.jar package.MyMainClass", shell=True, stdout=subprocess.PIPE , stdin=subprocess.PIPE)
print '1- create ok'
p.stdin.write('comand parameter1 parameter2')
print '2- writeComand ok'
p.stdin.flush()
print '3- flush ok'
p.stdin.close()
p.stdout.close()
print 'end'

the output is

1- create ok
2- writeComand ok
3- flush ok
end

and show show the dialog.

the line p.stdout.readline() spoils the script, as I can fix this?

Thank you very much any help.

1 Answer 1

1

Flush your System.out after printing just one result.

Additionally change your code to do this:

p = subprocess.Popen("java -cp MyProgram.jar package.MyMainClass",
    shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE)
p.stdin.write(command1)
p.stdin.flush()  # this should trigger the processing in the Java process
result = p.stdout.readline()  # this only proceeds if the Java process flushes
p.stdin.write(command2)
p.stdin.flush()
result = p.stdout.readline()
# and afterwards:
p.stdin.close()
p.stdout.close()
Sign up to request clarification or add additional context in comments.

13 Comments

in the java code? The java code works fine. I run it from the console and get the expected results. The problem is with the python script. thanks
Yes, in the Java code. When writing to a terminal, different buffering is applied by all processes. When writing to a pipe (instead of a tty), the Java process assumes that this is a bulk data connection and flushes only when the buffer is exhausted. Insert that flushing and try again.
I tried that and not make it. The problem is with the python code. I can not run the commands in the java interpreter. thanks
Aww, come on. That flushing is definitely necessary, believe me. Been there, done that hundreds of times. And it cannot do any harm. I'll have a look into that communicate again to see what we should use instead, just a moment.
I changed my answer. Give it a try :-)
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.