Home > Backend Development > Python Tutorial > java直接调用python脚本的例子

java直接调用python脚本的例子

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-06 11:29:13
Original
1471 people have browsed it

代码如下:


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

java直接调用python脚本的例子

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
Install JAVA
From 1970-01-01 08:00:00
0
0
0
Unable to install java
From 1970-01-01 08:00:00
0
0
0
Can java be used as the backend of the web?
From 1970-01-01 08:00:00
0
0
0
Is this in Java language?
From 1970-01-01 08:00:00
0
0
0
Help: JAVA encrypted data PHP decryption
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template