I am working on a small java app that needs to start a python script and interact with it. The python script is to run in the background and wait for commands. After each command I expect a response which will be forwarded back to the java app.
I have used the examples here and here to open the python script.
My question is how do I, without re-running the python script hook into it and run my commands?
public void startProcess()
{
try {
p = Runtime.getRuntime().exec("python " + scriptPath);
} catch (IOException e) {
e.printStackTrace();
}
}
public String executeCommand(String cmd)
{
String consoleResponse = "";
try {
// how do I perform something similar to p.exec(cmd)
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(p.getInputStream()));
BufferedReader stdError = new BufferedReader(new
InputStreamReader(p.getErrorStream()));
// read the output from the command
System.out.println("Here is the standard output of the command:\n");
while ((consoleResponse += stdInput.readLine()) != null) {
}
// read any errors from the attempted command
System.out.println("Here is the standard error of the command (if any):\n");
while ((consoleResponse = stdError.readLine()) != null) {
}
} catch (IOException e) {
e.printStackTrace();
}
return consoleResponse;
}
EDIT: The python script is for BACpypes. The script does 3 things. WhoIs: gets a list of all devices connected over bacnet ReadHexFile: reads in a text file to be sent to all devices on the network SendFile: sends the file to all devices.
I am not experienced with python and feel it would be simpler to keep all this data in one script.
I suppose one option is to break each command into its own script and pass the data to the java application.