我正在开发一个小的java应用程序,需要启动python脚本并与之交互。python脚本将在后台运行并等待命令。在每个命令之后,我期望一个响应,它将被转发回java应用程序。
我的问题是,如何在不重新运行python脚本钩子的情况下运行我的命令?
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;
}
编辑: python脚本是针对BACpypes的。这个脚本做了3件事。SendFile:获取通过be连接的所有设备的列表。ReadHexFile:读取要发送到网络上所有设备的文本文件。WhoIs:将文件发送到所有设备。
我没有使用python的经验,我觉得在一个脚本中保存所有这些数据会更简单。
我认为一种选择是将每个命令分解成其自己的脚本,并将数据传递给java应用程序。
https://stackoverflow.com/questions/51540597
复制相似问题