java调用python脚本

来源:互联网 发布:香港中文大学 知乎 编辑:程序博客网 时间:2024/05/22 06:08

这篇博客旨在吐血分享今天遇到的java调用python脚本遇到的坑,折腾了3个多小时终于可以跑通了,代码超级短,但网上的好多资料都是抄来抄去的,很少有能够直接跑通的,尤其是针对你的python文件中用到第三方类库的情况。

先来说说我为什么要用java调用python代码,原因就在于python在爬虫方面提供了丰富的类库,但我本来已经有一套java操作数据库以及消息中间件的代码,这些代码用python实现的话可能需要至少一周时间,为了减少时间成本,因此我决定用java调用python脚本的方式使用requests等等库,下面把我解决的过程分享出来,希望能给遇到类似问题的朋友一点帮助。

常见的java调用python脚本方式

  • 通过jython.jar提供的类库实现
  • 通过Runtime.getRuntime()开启进程来执行脚本文件

通过jython.jar提供的类库实现

通过jython.jar实现的话,我们需要引入jar包,具体我写了一个demo,假设你的python代码为test.py:

def my_test(name, age):    print("name: "+name)    print("age: "+age)    return "success"

java调用test.py代码:

public static void main(String[] args) {      PythonInterpreter interpreter = new PythonInterpreter();        interpreter.execfile("E:\\workspace\\pycharm_workspace\\weixincrawer\\test.py");        PyFunction function = (PyFunction)interpreter.get("my_test",PyFunction.class);        PyObject pyobject = function.__call__(new PyString("huzhiwei"),new PyString("25"));       System.out.println("anwser = " + pyobject.toString());      }

输出结果:

name: huzhiweiage: 25anwser = success

到此是没有什么问题的,我们使用function.call方法传入参数调用python函数,使用pyobject.toString()方法拿到python中my_test函数的返回值,但是如果你把test.py稍微做下修改如下:

import requestsdef my_test(name, age):    response = requests.get("http://www.baidu.com")    print("name: "+name)    print("age: "+age)    return "success"

不修改java调用代码的情况下,你会得到下面异常信息:

ImportError: No module named requests

没错,这就是我要讨论的问题,因为jython不可能涵盖所有python第三方类库的东西,所以在我们得python文件中用到requests类库的时候,很显然会报找不到模块的错误,这个时候我们是可以通过Runtime.getRuntime()开启进程来执行python脚本文件的。

通过Runtime.getRuntime()开启进程来执行脚本文件

使用这种方式需要同时修改python文件以及java调用代码,在此我同样在上面test.py的基础上进行修改:

import requestsimport sysdef my_test(name, age):    response = requests.get("http://www.baidu.com")    print("url:"+response.url)    print("name: "+name)    print("age: "+age)    return "success"my_test(sys.argv[1], sys.argv[2])

和上面test.py代码最大的区别在于,我们此处开启进程的方式实际上是在隐形的调用dos界面进行操作,因此在python代码中我们需要通过sys.argv的方式来拿到java代码中传递过来的参数。

java调用代码部分:

public static void main(String[] args) {      String[] arguments = new String[] {"python", "E:\\workspace\\pycharm_workspace\\weixincrawer\\test.py", "huzhiwei", "25"};        try {            Process process = Runtime.getRuntime().exec(arguments);            BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));            String line = null;            while ((line = in.readLine()) != null) {                System.out.println(line);            }            in.close();            int re = process.waitFor();            System.out.println(re);        } catch (Exception e) {            e.printStackTrace();        }      }

结果输出:

url:http://www.baidu.com/name: huzhiweiage: 250

在此需要注意的一点,java代码中的process.waitFor()返回值为0表示我们调用python脚本成功,返回值为1表示调用python脚本失败,这和我们通常意义上见到的0与1定义正好相反。

原创粉丝点击