[英]Running python with command line in java
程序卡在p2.waitFor()
(我测试前后打印字符串)
public void score() {
this.toXML();
try {
Process p = Runtime
.getRuntime()
.exec("python sumocfg_maker.py Carrefour.net.xml Detectors.det.xml edgedata.csv -ef");
p.waitFor();
Process p2 = Runtime.getRuntime().exec("python simulation.py");
new Thread(new Runnable() {
public void run() {
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = null;
try {
while ((line = input.readLine()) != null)
System.out.println(line);
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
p2.waitFor();
} catch (Exception e) {
e.printStackTrace();
}
}
和simulation.py
是
import os
os.system('cmd /c "sumo -c Simulation.sumocfg --duration-log.statistics --log duration.txt)
simulation.py
可以自行运行良好。 当我将命令放在 java 中的simulation.py
中时,我遇到了同样的问题。
System.out.println(line);
打印出“成功”,然后我没有遗漏simulation.py
中的代码,该代码保存了java在p2.wait()
之后立即读取的文件,并且没有p2.wait()
文件永远不会改变。
你有
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
但是你需要
BufferedReader input = new BufferedReader(new InputStreamReader(p2.getInputStream()));
由于 p 已经完成,缓冲读取器将等待但永远不会收到任何东西。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.