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写的英文字符大小写转换代码示例

几行代码的小工具,用于进行如下转换 TRANSACTIONS ON CLOUD COMPUTING =》 Transactions On Cloud Computing 复制代码 代码如...

Centos下实现安装Python3.6和Python2共存

写在前面 centos6.8中默认自带的python版本为python2.6,那么这里需要将其改为python3 下载并解压 官方下载地址为 https://www.python.o...

python 实现矩阵填充0的例子

需求: 原矩阵 [[1 2 3] [4 5 6] [7 8 9]] 在原矩阵元素之间填充元素 0,得到 [[1. 0. 2. 0. 3.] [0. 0. 0. 0. 0....

使用python统计文件行数示例分享

复制代码 代码如下:import time def block(file,size=65536):    while True:  &n...

Python作用域用法实例详解

本文实例分析了Python作用域用法。分享给大家供大家参考,具体如下: 每一个编程语言都有变量的作用域的概念,Python也不例外,以下是Python作用域的代码演示: def sc...