Posted in Python onFebruary 16, 2014
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脚本的例子
声明:登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。
Reply on: @reply_date@
@reply_contents@