java直接调用python脚本的例子

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

相关文章

Python批处理删除和重命名文件夹的实例

1. 删除当前目录下不含有指定文件类型的文件夹 #!/usr/bin/python # -*- coding: UTF-8 -*- import sys import os imp...

Python中处理时间的几种方法小结

从一个字符串开始 在CODE上查看代码片派生到我的代码片 >>>time_str='2008-08-08 08:08:08'  1.1.转换...

关于Python中空格字符串处理的技巧总结

关于Python中空格字符串处理的技巧总结

前言 大家应该都知道字符串处理,是任何语言最常用到的。 其中就经常会碰到,对字符串中的空格处理,比如:去除前后空格,去除全部空格,或者以空格为分隔符来处理。 好在Python中字符串有很...

Python Tkinter 简单登录界面的实现

Python Tkinter 简单登录界面的实现

如下所示: from tkinter import * class Reg (Frame): def __init__(self,master): frame = F...

深入理解 Python 中的多线程 新手必看

示例1 我们将要请求五个不同的url: 单线程 import time import urllib2 defget_responses(): urls=[ ‘http...