I know similar questions have been asked but after trying multiple different configurations I still have not quite found the solution I'm looking for. When I run my python file from java it prints all generated lines at once rather than printing as they execute.
I have a java program which displays a simple UI, once the user inputs all the arguments needed, I pass this information along to a working set of python scripts. Because the python program is quite extensive and takes a while to run there is a series of updated status messages which will print to the command prompt. In order to test running a process in Java I set up two simple test files.
My test java is as follows
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec("python -u Test.py");
BufferedReader br = new BufferedReader(new InputStreamReader(proc.getInputStream()));
String line = "";
while((line = b2.readLine()) != null){
System.out.println(line);
}
My Test.py file looks like (edit) I added the Unbuffered class as shown in the linked example and still no luck.
class Unbuffered(object):
def __init__(self,stream):
self.stream = stream
def write(self, data):
self.stream.write(data)
self.stream.flush()
def __getattr__(self.stream, attr):
return getattr(self.stream, attr)
sys.stdout = Unbuffered(sys.stdout)
print "Hello World"
time.sleep(5)
print "Hello again!"
When I run my java file, it waits 5 seconds before displaying both print lines from the python file. But I want it to not block the process and print out each line as its reached in the python file. Any help would be appreciated.
EDIT: (SOLVED)
My problem was I had to thread the new process outside of my main program. I ended up with something like
public class MyRunnable implements Runnable{
public void run(){
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec("python Test.py");
StreamGobbler errGobbler = new StreamGobbler(proc.getErrorStream(), "Error");
StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream(), "Output");
errGobbler.start();
outputGobbler.start()
}
public static void main(String[] args){
MyRunnable myRun = new MyRunnable();
Thread myThread = new Thread(myRun);
myThread.start();
}