java调用python程序

极少数时候,我们会碰到类似这样的问题:与 A 同学合作写代码, A 同学只会写 Python,不熟悉 Java ,而你只会写 Java 不擅长 Python,并且发现难以用 Java 来重写对方的代码,这时,就不得不想方设法“调用对方的代码”。
下面举一些简单的小例子,借此说明:如何在 Java 中调用 Python 代码。
Jython(原 JPython ),可以理解为一个由 Java 语言编写的 Python 解释器。
要使用 Jython, 只需要将 Jython-x.x.x.jar 文件置于 classpath 中即可 –> 官网下载,百度网盘。当然,通过 Maven 导入也 OK

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

一个 HelloPython 程序

import org.python.util.PythonInterpreter;

public class HelloPython {
    public static void main(String[] args) {
        PythonInterpreter interpreter = new PythonInterpreter();
        interpreter.exec("print('hello')");
    }
}

什么是 PythonInterpreter 呢?它的中文意思即“ Python 解释器”。我们知道 Python 程序都是通过解释器执行的,上面的代码在 JVM 中创建一个“ Python 解释器”对象,模拟 Python 解释器的行为,通过 exec(” Python 语句”) 直接在 JVM 中执行 Python 代码,代码的输出结果为:hello,该程序运行速度相较正常的 Java or Python 程序都要慢那么一点。
在 JVM 中执行 Python 脚本

interpreter.execfile("D:/labs/mytest/hello.py");  

如上,将 exec 改为 execfile 就可以了。需要注意的是,这个 .py 文件不能含有第三方模块,因为这个“ Python 脚本”最终还是在 JVM 环境下执行的(而非依赖于本地计算机环境),如果 .py 程序中有用到第三方模块(例如 NumPy)将会报错:java ImportError: No module named xxx
在 JVM 中调用 Python 编写的函数
先写一个 hello.py 的 Python 代码:

def hello():
    return 'Hello'

在 Java 代码中调用这个 Python 函数:

import org.python.core.PyFunction;
import org.python.core.PyObject;
import org.python.util.PythonInterpreter;

public class HelloPython {
    public static void main(String[] args) {
        PythonInterpreter interpreter = new PythonInterpreter();
        interpreter.execfile("D:/labs/hello.py");

        PyFunction pyFunction = interpreter.get("hello", PyFunction.class); // 第一个参数为期望获得的函数(变量)的名字,第二个参数为期望返回的对象类型
        PyObject pyObject = pyFunction.__call__(); // 调用函数

        System.out.println(pyObject);
    }
}

上面的代码执行结果为:Hello
即便只是调用一个函数,也必须先加载这个 .py 文件,之后再通过 Jython 包中所定义的类获取、调用这个函数。
如果函数需要参数,在 Java 中必须先将参数转化为对应的“ Python 类型”(姑且可以称作 Jython 类型 (●’◡’●),例如:

__call__(new PyInteger(a), new PyInteger(b))

a,b的类型均为 Java 中的 int 型,还有一些 Jython 类型诸如:PyString(String string)、PyList(Iterator<PyObject> iter) 等,详细信息可以参考官方的 api 文档。
在本地环境中调用 Python 脚本
由于 Jython 运行过慢并且不支持第三方的 Python 模块,通过 Java 代码执行一段终端命令来调用 Python 脚本可能才是实际中真正会用到的方式。
下面是和舍友合作写的一个小程序(可以识别很粗的手写数字),界面上引用了 core java 里的一段代码:

import java.io.*;

class PyCaller {
    private static final String DATA_SWAP = "temp.txt";
    private static final String PY_URL = System.getProperty("user.dir") + "\\test.py";

    public static void writeImagePath(String path) {
        PrintWriter pw = null;
        try {
            pw = new PrintWriter(new FileWriter(new File(DATA_SWAP)));
        } catch (IOException e) {
            e.printStackTrace();
        }

        pw.print(path);
        pw.close();
    }

    public static String readAnswer() {
        BufferedReader br;
        String answer = null;
        try {
            br = new BufferedReader(new FileReader(new File(DATA_SWAP)));
            answer = br.readLine();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return answer;
    }

    public static void execPy() {
        Process proc = null;
        try {
            proc = Runtime.getRuntime().exec("python " + PY_URL);
            proc.waitFor();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    // 测试码
    public static void main(String[] args) throws IOException, InterruptedException {
        writeImagePath("D:\\labs\\mytest\\test.jpg");
        execPy();
        System.out.println(readAnswer());
    }
}

运行流程:Java Swing 界面接收用户输入 –> Java 将用户输入写到本地文件中 –> Java 调用本地 Python 脚本 –> Python 从本地文件拿到用户输入 –> Python 处理用户输入得到最终结果 –> Python 把最终结果写到本地文件 –> Java 对 Python 脚本的调用结束 –> Java 从本地文件中取出最终结果 –> Java 把最终结果返回给用户
注意:在用jython在eclipse中开发的时候,控制台的输出可能

console: Failed to install ”: java.nio.charset.UnsupportedCharsetException: cp0.

样一个错误,此时如果控制台的错误堆栈中有中文的时候,中文就会显示为乱码,解决办法为在要执行的代码上右键,
Run As>Run Configurations,选择第二个页签Arguments,在VM arguments中添加

-Dpython.console.encoding=UTF-8

即可,然后Apply>Run就行了.

猜你喜欢

转载自blog.csdn.net/qq_32447301/article/details/80908793