See: Calling Java from Python
" You can also use Py4J. The first page has an example and a lot of documentation, but essentially you just call Java methods from your Python code, as if they were python methods:
from py4j.java_gateway import JavaGateway
gateway = JavaGateway () # connect to the JVM
java_object = gateway.jvm.mypackage.MyClass () # call constructor
other_object = java_object.doThat ()
other_object.doThis (1, 'ABC')
gateway.jvm.java.lang.System.out.println ('Hello World!') # call a static method
Unlike Jython, one part of Py4J works in the Python VM, so it is always "updated" with the latest version of Python, and you can use libraries that do not work on Jython (for example, lxml). The other part runs on the Java virtual machine that you want to invoke.
Communication is via sockets instead of JNI, and Py4J has its own protocol (to optimize certain cases, to manage memory, etc.) "
source share