Pingchas 发表于 2025-7-24 17:31:16

Java使用CMD运行python程序

先将python.exe配置到环境变量
Process proc;
      try {
            try {
                proc = Runtime.getRuntime().exec("python d://test.py");
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
            String line = null;
            while ((line = in.readLine()) != null) {
                System.out.println(line);
            }
            in.close();
            proc.waitFor();
      } catch (IOException e) {
            e.printStackTrace();
      } catch (InterruptedException e) {
            e.printStackTrace();
      }

alglsf666 发表于 2025-7-25 14:49:40

谢谢分享

H.U.C清风 发表于 2025-7-25 19:10:29

谢谢分享,已回复
页: [1]
查看完整版本: Java使用CMD运行python程序