Hi,
I hope I didn't totally misname this post... Anyway, I'm trying to create a Java GUI that links together multiple codes that are around the lab I'm working in. Unfortunately, one of the main ones is in python, so I'm trying to execute the python code by opening a command line process then sending this process my command to run the file "python 3DVis.py -passive" as I would from the command prompt. As the command isn't an executable, I can't run it directly from runtime.exec() and this is all I could think of doing, though I suppose there may be a more elegant solution to this than I have come up with, in which case I'd appreciate any suggestions you may have.
The thing is that this part all works fine and I can start the python program running through the command process, but the issue is when I try to read the output that the python program returns to the command prompt. I am trying to interact with the python program's input/output streams through the command process, but it seems that the buffered reader doesn't read the python program's output as a full line. Once the python program terminates and the command process displays the current C:\Documents and Settings\.....> prompt, all the python programs output is displayed as it should, but not before.
Here's an example of the relevant code to set up the process and python program:
try
{
Process =Runtime.getRuntime().exec("cmd.exe");
OS = new DataOutputStream(Process.getOutputStream());
IS = new DataInputStream(Process.getInputStream());
OS.write(new String("cd C:\\Documents and Settings\\...\\Demos\n").getBytes());
OS.write(new String("set path=%path%;C:\\python25\n").getBytes());
OS.write(new String("python 3DVis.py -passive\n").getBytes());
OS.flush();
}
catch (Exception e)
{
System.out.println(e);
e.printStackTrace();
}
...and to listen to the program:
while(true)
{
try
{
byte[] buf = new byte[1000];
int length = IS.read(buf);
String inputString = new String(buf, 0, length);
// BufferedReader br = new BufferedReader(new InputStreamReader(visIS));
// String inputString = br.readLine();
System.out.println(inputString);
}
catch (Exception e)
{
e.printStackTrace();
}
}
I tried both the buffered reader and the DataInputStream methods but both had the same problem. It seems like the end of line for the command process output doesn't occur except after the prompts, and so the readers don't consider the python output as a complete line...
Any thoughts or suggestions would be greatly appreciated! Thanks.