为python进程指定名称,并在java程序中调用启动和关闭
为python程序指定进程名称(java同理)
将python.exe复制一份改为 python_fz.exe,
执行的时候调用 python_fz xxx即可
编写执行python的bat文件
@echo off
title tianshihao
start python_fz C:/postal-logistics/core/new1.py
exit;
编写java代码调用bat文件
public R startEngine() throws IOException {
Runtime.getRuntime().exec("C:/postal-logistics/bat/start_fz.bat");
return R.ok("启动引擎");
}
public R stopEngine(){
try {
Process exec = Runtime.getRuntime().exec("taskkill /im python_fz.exe /f ");
BufferedReader in1 = new BufferedReader(new InputStreamReader(exec.getInputStream(),
StandardCharsets.UTF_8));
String line1;
while ((line1 = in1.readLine()) != null) {
System.out.println(line1);
}
} catch (IOException e) {
e.printStackTrace();
return R.failed("停止引擎失败");
}
return R.ok("停止引擎成功");
}