问题描述
程序卡在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 已经完成,bufferedreader 将等待但永远不会收到任何东西。