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

相关文章

Pandas时间序列重采样(resample)方法中closed、label的作用详解

Pandas提供了便捷的方式对时间序列进行重采样,根据时间粒度的变大或者变小分为降采样和升采样: 降采样:时间粒度变大。例如,原来是按天统计的数据,现在变成按周统计。降采样会涉及到...

python协程之动态添加任务的方法

python协程之动态添加任务的方法

python协程只能运行在事件循环中,但是一旦事件循环运行,又会阻塞当前任务。所以只能在当前进程中再开一个线程,这个线程的主要任务是运行事件循环,就是event_loop,因为他是一个无...

Python查找函数f(x)=0根的解决方法

本文实例讲述了Python查找函数f(x)=0根的解决方法。分享给大家供大家参考。具体实现方法如下: ''' root = ridder(f,a,b,tol=1.0e-9). F...

Python读取环境变量的方法和自定义类分享

使用os.environ来读取和修改环境变量: 复制代码 代码如下: import os print (os.environ["TEMP"]) mydir = "c:\\mydir" o...

python中pass语句用法实例分析

本文实例讲述了python中pass语句用法。分享给大家供大家参考。具体分析如下: 1、空语句 do nothing 2、保证格式完整 3、保证语义完整 4、以if语句为例: C/C++...