Hailiang Zhang
Hailiang Zhang

Reputation: 18870

Communication between Java and Python through stdout

I have a Java code calling Python through an asynchronous process, and I want the Python process to listen to stdout from Java until something printed.

Following is the java code:

import java.io.*;

public class host {
    public static void main(String[] args) throws Exception{
        System.out.println("java starts at " + System.currentTimeMillis() + " ms");
        ProcessBuilder pb = new ProcessBuilder("python","a.py");
        Process tr = pb.start();
        System.out.println("First msg");
        System.out.println("Second msg");
        Thread.sleep(3000);
        System.out.println("x");
        System.out.println("java ends at " + System.currentTimeMillis() + " ms");
    }
}

and following is the python code:

import sys
import time
if __name__=="__main__":
    fo=open("a.txt","w")
    fo.write('python starts at: %.0f\n'%(time.time()*1000))
    line = sys.stdout.readline()
    while(line != "x\n"):
        fo.write(line+"\n")
        line = sys.stdout.readline()
    fo.write('python ends at: %.0f\n'%(time.time()*1000))
    fo.close()

However, the Python process seemed not be able to capture the stdout from Java. Since I am new to Java, I am not sure whether there is something fundamentally wrong or not, and whether the above model provides an effective way between Java and Python communication.

EDIT (UPDATE)

Based on the answers/comments, I modified my Java code as follows:

import java.io.*;

public class host {
    public static void main(String[] args) throws Exception{
        System.out.println("java starts at " + System.currentTimeMillis() + " ms");
        ProcessBuilder pb = new ProcessBuilder("python","a.py");
        Process tr = pb.start();
        PrintStream ps = new PrintStream(tr.getOutputStream());
        ps.println("First msg"); ps.flush();
        ps.println("Second msg"); ps.flush();
        Thread.sleep(3000);
        ps.println("x"); ps.flush();
        System.out.println("java ends at " + System.currentTimeMillis() + " ms");
    }
}

and changed python script as follows:

import sys
import time
if __name__=="__main__":
    fo=open("a.txt","w")
    fo.write('python starts at: %.0f\n'%(time.time()*1000))
    line = sys.stdin.readline()
    while(line != "x\n"):
        fo.write(line+"\n")
        line = sys.stdin.readline()
    fo.write('python ends at: %.0f\n'%(time.time()*1000))
    fo.close()

And following is the output:

python starts at: 1453263858103
First msg

First msg

Second msg

python ends at: 1453263863103

Upvotes: 0

Views: 1895

Answers (2)

njzk2
njzk2

Reputation: 39406

In java, write to the process you created:

ProcessBuilder pb = new ProcessBuilder("python","a.py");
Process tr = pb.start();
tr.getOutputStream().println("First msg");

In python, read from stdin:

line = sys.stdin.readline()

Upvotes: 1

rotten
rotten

Reputation: 1630

The receiving process should listen to stdin, not stdout.

Then pipe the output from java to python:

java -jar myjava.jar | python mylistener

Upvotes: 2

Related Questions