在 Java 中调用 Python 代码

 极少数时候,我们会碰到类似这样的问题:与 A 同学合作写代码, A 同学只会写 Python,不熟悉 Java ,而你只会写 Java 不擅长 Python,并且发现难以用 Java 来重写对方的代码,这时,就不得不想方设法“调用对方的代码”。

下面举一些简单的小例子,借此说明:如何在 Java 中调用 Python 代码。

 

主要内容如下:

  1. 什么是 Jython ?
  2. 一个 HelloPython 示例程序
  3. 如何在 JVM 中执行 Python 脚本
  4. 在 JVM 中调用 Python 编写的函数
  5. 在本地环境中调用 Python 脚本

 

什么是 Jython? 

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 把最终结果返回给用户

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值