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.