Java调用python脚本

Java调用python脚本

1.java调用python脚本需要运行python的基本环境

2.Process process = Runtime.getRuntime().exec(cmdArr);
在java代码中使用上述代码调用python脚本即可,其中cmdArr相当于在cmd
调用python脚本时的指令。

3.其原理是建立一个新的进程来运行python代码。

4.cmdArr是以字符串数组的形式。

实例代码

public static void main(String[] args) throws IOException, InterruptedException {
        //pychon的环境路径
        String exe = "python ";
        // 存储pychon脚本的存储路径
        String command = "F:\\YOLOv4-Hat-detection-master\\detect.py";
        // 定义传入Python脚本的命令行参数,将参数放入字符串数组里
        String num1 = "--source 1.mp4";
        String num2 = "--number camera1";
        String[] cmdArr = new String[] {exe, command};
        //使用Runtime.getRuntime()方法去调用pychon程序
        Process process = Runtime.getRuntime().exec(cmdArr);
        //取得命令结果的输出流
        InputStream is = process.getInputStream();
        //声明输出流对象,将结果显示出来
        DataInputStream dis = new DataInputStream(is);
        String str = dis.readLine();
        System.out.println(str);
        System.out.println(process.waitFor());
    }
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值