Call non-native Java code from Python
Question: I want to be able to call certain methods and such that are contained in a
Java jar that is already running (It is guaranteed that it will be running). I
have found things like Jython, but those only seem to be able to access Java's
native classes and such.


Answer: **Check out this:[Calling Java from
Python](http://stackoverflow.com/questions/3652554/calling-java-from-python)**

"**You could also use Py4J. There is an example on the frontpage and lots 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() # invoke constructor

>>>

>>> other_object = java_object.doThat()

>>>

>>> other_object.doThis(1,'abc')

>>>

>>> gateway.jvm.java.lang.System.out.println('Hello World!') # call a static
method

As opposed to Jython, one part of Py4J runs in the Python VM so it is always
"up to date" with the latest version of Python and you can use libraries that
do not run well on Jython (e.g., lxml). The other part runs in the Java VM you
want to call.

**The communication is done through sockets instead of JNI and Py4J has its
own protocol (to optimize certain cases, to manage memory, etc.)** "

