栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java调用执行python脚本

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java调用执行python脚本

Java调用执行python脚本
    • 一、python脚本执行方式
    • 二、Java调用执行python脚本
    • 三、关闭停止进程(端口)命令
    • 四、java调用执行关闭进程杀掉相关进程

一、python脚本执行方式

1、python脚本执行,可以直接进入到脚本目录的终端窗口cmd下执行命令如

python test.py

2、电脑安装了python后,未配置环境变量,知道python按照目录在哪里的也可打开终端窗口cmd运行名令如下:(python执行器路径 python脚本路径)

D:PythonPython37python.exe D:pythontest.py

3、运行脚本如果想将输出写入到指定文件夹下:

D:PythonPython37python.exe D:pythontest.py > test.log  ## 不追加形式写入
D:PythonPython37python.exe D:pythontest.py >> test.log  ## 追加形式写入

问题:什么原因导致写入指定文件,控制台直接执行后,输出前一半内容到文件,后一半还在终端窗口???是否区分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();
        }
    }
    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();
        }
    }

问题:为什么用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

查找到相关的进程之后,再杀掉相关进程,即可关闭停止在运行的脚本服务,通常常见的是一个服务查出来的进程id只有一个所以只需/f参数,但若存在不一样的即存在子进程,则加/t同时杀掉相关子进程:

taskkill /f /im pythen.exe	// 杀掉所有python进程
taskkill /f /pid 20401  // 根据进程id杀掉进程
taskkill /f /t /pid 20401	// 根据进程id杀掉进程及子进程
/f代表强制关闭,/im代表指定需要关闭的程序,/t代表终止指定的进程和由它启用的子进程
四、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();
        }

    }

问题:除了这种方法是否还有别的方法思路可以执行python及其他脚本或查找到相关的进程服务并结束它???

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/893956.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号