首页 > 编程语言 > 详细

python写脚本并用java调用python(三)

时间:2016-06-18 02:08:23      阅读:249      评论:0      收藏:0      [点我收藏+]

1)编写mytest.py完成一个简单加法计算

# coding:utf8

#def 表示一个方法 adder
def adder(a, b):
	return a+b

#这里执行adder方法并打印出结果
print adder(1,2)

?2)运行以上脚本方式如图
bubuko.com,布布扣

1+2 = 3 打印成功!

3)java调用python脚本的两种方式

Process process = Runtime.getRuntime().exec("python E:\\mytest.py");
        InputStreamReader ir = new InputStreamReader(process.getInputStream());
        LineNumberReader input = new LineNumberReader(ir);
        String line;
        while((line = input.readLine()) != null)
            System.out.println(line);
        input.close();
        ir.close();

结果如下图展示:该方法目前没找到可以给python传参,但是支持运行python脚本中的三方类库
bubuko.com,布布扣
通过jython-standalone-2.7.0.jar调用python

 PythonInterpreter interpreter = new PythonInterpreter();  
        interpreter.execfile("E:\\test.py");  
        PyFunction func = (PyFunction)interpreter.get("retHtml",PyFunction.class); 
        PyObject pyobj = func.__call__(new PyInteger(2016),new PyInteger(2016));  
        System.out.println("retMsg = " + pyobj);

?

?

?

?

?

?

?

python写脚本并用java调用python(三)

原文:http://zliguo.iteye.com/blog/2305245

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!