java直接调用python脚本的例子

2019/7/13 21:56:12

本文主要是介绍java直接调用python脚本的例子,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

复制代码 代码如下:

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main {
 public static void main(String[] args) {
  try {
   System.out.println("start");
   Process pr = Runtime.getRuntime().exec("python test.py");

   BufferedReader in = new BufferedReader(new InputStreamReader(
     pr.getInputStream()));
   String line;
   while ((line = in.readLine()) != null) {
    System.out.println(line);
   }
   in.close();
   pr.waitFor();
   System.out.println("end");
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
}

如果在eclipse中直接运行报如下错误:

java.io.IOException: Cannot run program "python": CreateProcess error=2

则配置Run Configuration中的Enviroment,增加PATH变量,见下图:



这篇关于java直接调用python脚本的例子的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程