java 调用 python(一)

创建一个.py 文件用于测试

 

 

 处理java代码:

pom:

       <dependency>
            <groupId>org.python</groupId>
            <artifactId>jython-standalone</artifactId>
            <version>2.7.0</version>
        </dependency>
View Code

 

代码:

@Override
    public String run() {
        PythonInterpreter interpreter = new PythonInterpreter();
        interpreter.execfile("E:\\pyWorkSpace\\demo\\demo.py");
        PyFunction pyFunction = interpreter.get("demo", PyFunction.class);
        int a = 1, b = 2;
        //调用函数,如果函数需要参数,在Java中必须先将参数转化为对应的“Python类型”
        PyObject pyobj = pyFunction.__call__(new PyInteger(a), new PyInteger(b));
        return pyobj.toString();
    }

 

 

 

1+2=3 

 

posted @ 2020-05-29 10:40  DarGi  阅读(145)  评论(0编辑  收藏  举报