java调用python并传递参数

python程序:testpj.py

#coding:utf-8

import sys

print sys.argv[1]

java程序:Testpj.java

import java.io.BufferedReader;

import java.io.InputStreamReader;

public class Testpj{

public static void main(String[] args)throws Exception {

Process pr = Runtime.getRuntime().exec("python testpj.py 123");

//获取python文件运行后的输出

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("sucecss!");

   }

}

运行结果:

MacBook-Air:Downloads huangyong$ java Testpj

123

sucecss!

文件地址:

github.com/Inspiring26/wow/tree/master/java调用python

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容