在命令行调用python执行结果可以在指定位置生成所需结果文件,但在java中调用python只能获得同样的输出语句,但是不生成所需结果文件,原因不明
因为所调用的python脚本是3.8版本的(Jython只支持2),所以采用RunTime.getRuntime().exec命令行方式调用python脚本
源代码
public class LoadPython {
public static void main(String[] args) {
Process proc;
try {
proc = Runtime.getRuntime().exec("python E:\\develop\\tmp\\tang2\\documents\\customers\\dist\\GM_APP_InputTest.py runSimulation \'{\"baseData\":{\"userId\":100,\"age\":24,\"height\":175,\"weight\":98,\"tdi\":66,\"glycuresisType\":1,\"hba1c\":25},\"events\":[{\"cho\":{\"timeStart\":\"10:30\",\"timeEnd\":\"10:00\",\"type\":1,\"data\":20}}]}\'");// 执行py文件
//用输入输出流来截取结果
BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
String line = null;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
in.close();
proc.destroy();
} catch (IOException e) {
e.printStackTrace();
}
}
}
命令行响应结果
E:\develop\tmp\tang2\documents\customers\dist>python GM_APP_InputTest.py runSimulation '{"baseData":{"userId":100,"age":24,"height":175,"weight":98,"tdi":66,"glycuresisType":1,"hba1c":25},"events":[{"cho":{"timeStart":"10:30","timeEnd":"10:00","type":1,"data":20}}]}'
adult#average
Process ID: 13028
Simulation starts ...
Simulation Completed!
simulation time: 5.2851855754852295
idea信息栏运行输出结果
adult#average
Process ID: 6972
Simulation starts ...
Simulation Completed!
simulation time: 5.270181179046631
Process finished with exit code 0
两者运行输出结果一致,命令行可以生成这个adult#average.csv文件,但是java的runtime始终无法生成目标文件
在哪里做出修改,可以使java中调用python生成文件
1.首先确定你调用的python到底执行了没有,在python脚本里写点日志
目测你路径都写错了,/不是转义符,不要都写2遍,\才是转义符