如何解决在java中使用命令行运行python
程序卡在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 将等待但永远不会收到任何东西。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。