java直接调用python脚本的例子

yipeiwu_com5年前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 Socket实现简单TCP Server/client功能示例

本文实例讲述了Python Socket实现简单TCP Server/client功能。分享给大家供大家参考,具体如下: 网络上关于socket的介绍文章数不胜数。自己记录下学习的点点滴...

python里将list中元素依次向前移动一位

问题 定义一个int型的一维数组,包含10个元素,分别赋值为1~10, 然后将数组中的元素都向前移一个位置, 即,a[0]=a[1],a[1]=a[2],…最后一个元素的值是原来第一个元...

python实现海螺图片的方法示例

python实现海螺图片的方法示例

本文介绍了如何用python实现海螺图片,感兴趣的可以参考一下,具体代码如下: 代码如下: import turtle import time t = turtle.Turtl...

Pytorch中的VGG实现修改最后一层FC

https://discuss.pytorch.org/t/how-to-modify-the-final-fc-layer-based-on-the-torch-model/766/1...

用Python实现web端用户登录和注册功能的教程

用Python实现web端用户登录和注册功能的教程

用户管理是绝大部分Web网站都需要解决的问题。用户管理涉及到用户注册和登录。 用户注册相对简单,我们可以先通过API把用户注册这个功能实现了: _RE_MD5 = re.compil...