Java调用执行python脚本
-
- 一、python脚本执行方式
- 二、Java调用执行python脚本
- 三、关闭停止进程(端口)命令
- 四、java调用执行关闭进程杀掉相关进程
一、python脚本执行方式
1、python脚本执行,可以直接进入到脚本目录的终端窗口cmd下执行命令如
python test.py
- 1
2、电脑安装了python后,未配置环境变量,知道python按照目录在哪里的也可打开终端窗口cmd运行名令如下:(python执行器路径 python脚本路径)
D:\Python\Python37\python.exe D:\python\test.py
- 1
3、运行脚本如果想将输出写入到指定文件夹下:
D:\Python\Python37\python.exe D:\python\test.py > test.log ## 不追加形式写入
D:\Python\Python37\python.exe D:\python\test.py >> test.log ## 追加形式写入
- 1
- 2
问题:什么原因导致写入指定文件,控制台直接执行后,输出前一半内容到文件,后一半还在终端窗口???是否区分python执行器输出或者脚本输出等???
二、Java调用执行python脚本
python调用脚本的思路即用Runtime.getRuntime().exec()调用终端窗口cmd执行相应命令,其方法如下进入python脚本的目录,执行命令:
public static void callPython(String fileName){
// cmd 命令进入脚本路径
String CMD_CD = "cmd /c D: && cd D:\\Python && ";
String CMD_PYTHON = "&& D:\\Python\\Python37\\python.exe test.py";
Process process;
BufferedReader bufferedReader;
try {
process = Runtime.getRuntime().exec(CMD_CD + CMD_PYTHON);
bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream(), Charset.forName("GBK")));
String res ;
while ((res = bufferedReader.readLine()) != null) {
System.out.println(res);
}
} catch (IOException e) {
e.printStackTrace();
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
public static void callPythonByPath(String fileName){
// cmd 命令进入脚本路径
String pythonExe = "D:\\Python\\Python37\\python.exe ";
String scriptPath = "D:\\Python\\test.py";
Process process;
BufferedReader bufferedReader;
try {
process = Runtime.getRuntime().exec(pythonExe + scriptPath);
bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream(), Charset.forName("GBK")));
String res ;
while ((res = bufferedReader.readLine()) != null) {
System.out.println(res);
}
} catch (IOException e) {
e.printStackTrace();
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
问题:为什么用Java调用python脚本执行输出写入到指定文件无效果???有其他方法建议吗???
三、关闭停止进程(端口)命令
有些脚本执行后相当于开启了服务会使用某个端口号,当我们不停止它或者关电脑,那个端口号会一直被占用,因此将要通过查找到该端口的进程id,并通过进程id将该进程杀死。
查看进程的最常见的方式是通过端口号查询,此外,用Java调用执行python后不知道它起的是什么端口号,这时可以通过查看脚本执行命令的形式去查找相关的进程信息
netstat -ano |findstr "8080"
wmic process where name="python.exe" get processid, commandline
// 要注意带路径的话单引号里面的反斜杠要转义
wmic process where 'caption="python.exe" and commandline="D:\\Python\\Python37\\python.exe D:\\Python\\test.py"' get processid
wmic process where 'commandline like "%D:\\Python\\test.py%"' get processid
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
查找到相关的进程之后,再杀掉相关进程,即可关闭停止在运行的脚本服务,通常常见的是一个服务查出来的进程id只有一个所以只需/f参数,但若存在不一样的即存在子进程,则加/t同时杀掉相关子进程:
taskkill /f /im pythen.exe // 杀掉所有python进程
taskkill /f /pid 20401 // 根据进程id杀掉进程
taskkill /f /t /pid 20401 // 根据进程id杀掉进程及子进程
/f代表强制关闭,/im代表指定需要关闭的程序,/t代表终止指定的进程和由它启用的子进程
- 1
- 2
- 3
- 4
四、java调用执行关闭进程杀掉相关进程
思路同样是用Runtime.getRuntime().exec()调用终端窗口cmd执行相应命令查找相关进程然后将该进程强制结束掉:
public static void killProcessid(String port) {
// cmd 命令进入脚本路径
Process process;
BufferedReader bufferedReader;
String pid = "";
String [] args = new String[]{"cmd.exe", "/c", "netstat -ano|findstr", port};
try {
process = Runtime.getRuntime().exec(args);
bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream(), Charset.forName("GBK")));
String res = bufferedReader.readLine();
while (res != null) {
String[] strs = res.split("\\s");
pid = strs[strs.length - 1];
}
process = Runtime.getRuntime().exec("taskkill /f /t /pid" + pid + "");
bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream(), Charset.forName("GBK")));
while ((res = bufferedReader.readLine()) != null) {
System.out.println(res);
}
bufferedReader.close();
process.destroy();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void killProcessByCommandline(String scriptPath) {
Process process;
BufferedReader bufferedReader;
String pid = "";
try {
String s = "wmic process where ' caption=\"python.exe\" and commandline=\"D:\\\\Python\\\\Python37\\\\python.exe D:\\\\Python\\\\test.py\" ' get processid";
String CMD_PYTHON = "wmic process where 'commandline like \"%" + scriptPath.replace("\\", "\\\\") + "%\"'get processid";
process = Runtime.getRuntime().exec(CMD_PYTHON);
bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream(), Charset.forName("GBK")));
String res ;
while ((res = bufferedReader.readLine()) != null) {
String strs = res.trim(); // 去除输出发前后空格
if (!"".equals(strs) && !"processid".equals(strs)) {
pid = strs;
break;
}
}
process = Runtime.getRuntime().exec("taskkill /f /t /pid" + pid + "");
bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream(), Charset.forName("GBK")));
while ((res = bufferedReader.readLine()) != null) {
System.out.println(res);
}
bufferedReader.close();
process.destroy();
} catch (IOException e) {
e.printStackTrace();
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
问题:除了这种方法是否还有别的方法思路可以执行python及其他脚本或查找到相关的进程服务并结束它???