java如何调用python
1、首先,先写一个 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); //。

3、然后,上面的代码执行结果为:Hello,即便只是调用一个函数,也必须先加载这个 .py 文件,之后再通过 Jython 包中所定义的类获取、调用这个函数。

5、然后,在本地环境中调用Python脚本,由于 Jython运行过慢并且不支持第三方的 Python 模块,通过 Java 代码执行一段终端命令来调用 Python 脚本可能才是实际中真正会用到的方式。
