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

相关文章

PyCharm 2019.3发布增加了新功能一览

PyCharm 2019.3发布增加了新功能一览

Python的IDE(Integrated Development Environment 集成开发环境)非常多,如:VS Code、Sublime、NotePad、Python自带编辑...

python中format()函数的简单使用教程

python中format()函数的简单使用教程

先给大家介绍下python中format函数,在文章下面给大家介绍python.format()函数的简单使用 ---恢复内容开始--- python中format函数用于字符串的格式化...

python使用Tkinter显示网络图片的方法

本文实例讲述了python使用Tkinter显示网络图片的方法。分享给大家供大家参考。具体实现方法如下: ''' tk_image_view_url_io.py display an...

Python设计模式之代理模式实例详解

Python设计模式之代理模式实例详解

本文实例讲述了Python设计模式之代理模式。分享给大家供大家参考,具体如下: 代理模式(Proxy Pattern):为其他对象提供一种代理以控制对这个对象的访问 #!/usr/b...

利用Python校准本地时间的方法教程

利用Python校准本地时间的方法教程

1. 概念 1.1 基本概念 时间,对于我们来说很重要,什么时候做什么?什么时候发生什么?没有时间的概念,生活就乱了。 在日常的运维当中,我们更关注告警的时间:什么时候发生、什么事故...