java中调用python

来源:互联网 发布:5ghz网络设置 编辑:程序博客网 时间:2024/05/24 01:04

一、在java中调用python的步骤

1.下载jython,是java调用python的库。

     地址: http://www.jython.org/downloads.html

2.将下载的 jython 的jar包导入到项目中。

3.代码编写。(如下)

public class TestControlPython {    private static String PATH_PARSER = "python_apk_parse/apk_parse/apk_parse.py";    private static String PATH_ANDROGUARD_PARSER = "python_apk_parse/apk_parse/androguard/androgexf.py";    private static String PATH_GEXF_DIR = "python_apk_parse/apk_parse/androguard/androgexf.py";            public static void main(String[] args) {        getGexf();    }    public static void getGexf() {        //1.声明python解释器。        PythonInterpreter interpreter = new PythonInterpreter();        //2.设置要执行的python文件的路径。        interpreter.execfile(PATH_PARSER);        //3.找到python中的想要使用的相关方法。        PyFunction func = (PyFunction) interpreter.get("get_gexf", PyFunction.class);        //4.将java中数据类型 转为 python 中的数据类型。        PyString path = new PyString("src/tree/test/testxml/achute.pixelsnake-3.apk");        //5.调用方法,并且传递参数。        PyObject pyobj = func.__call__(path);    }}<span style="font-size:18px;"></span>

4.可能遇到的错误。

(1)console: Failed to install '': java.nio.charset.UnsupportedCharsetException: cp0.

   在运行虚拟机配置中输入 : -Dpython.console.encoding=UTF-8


1 0
原创粉丝点击