PythonTip >> 博文 >> python

Java直接调用python脚本

zihua 2014-02-16 18:02:46 点击: 2264 | 收藏


示例代码如下:

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变量,见下图:

原文链接:http://my.oschina.net/cloudcoder/blog/200067

作者:zihua | 分类: python | 标签: python | 阅读: 2264 | 发布于: 2014-02-16 18时 |
上一篇: 提示
下一篇: Python动态监控日志的内容