python:
p.stdin.write("haha")
java:
Scanner in = new Scanner(System.in)
data = in.next()
From the Java Scanner docs:
By default, a scanner uses white space to separate tokens. (White
space characters include blanks, tabs, and line terminators.
Your python code does not write anything that a Scanner recognizes as the end of a token, so the Scanner sits there waiting to read more data. In other words, next()
reads input until it encounters a whitespace character, then it returns the data read in, minus the terminating whitespace.
This python code:
import subprocess
p = subprocess.Popen(
[
'java',
'-cp',
'/Users/7stud/java_programs/myjar.jar',
'MyProg'
],
stdout = subprocess.PIPE,
stdin = subprocess.PIPE,
)
p.stdin.write("haha\n")
print "i am done"
print p.stdout.readline().rstrip()
...with this java code:
public class MyProg {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String data = in.next();
System.out.println("Java program received: " + data);
}
}
...produces this output:
i am done
Java program received: haha