Py4JPython和Java的互调接口
Py4J 使得 Python 程序可以利用 Python 解释器直接调用Java虚拟机中的 Java 对象,也可以让 Java 调用 Python 对象,有点像 Python 版的 JNI。
示例代码:
>>> from py4j.java_gateway import JavaGateway >>> gateway = JavaGateway() # connect to the JVM >>> random = gateway.jvm.java.util.Random() # create a java.util.Random instance >>> number1 = random.nextInt(10) # call the Random.nextInt method >>> number2 = random.nextInt(10) >>> print(number1,number2) (2, 7) >>> addition_app = gateway.entry_point # get the AdditionApplication instance >>> addition_app.addition(number1,number2) # call the addition method 9
评论