Table of Contents

Calling Java from Python

tldr

Libraries

Here is a list of some libraries that can be used to call Java functions and objects from Python.

py4j

Py4J enables Python programs running in a Python interpreter to dynamically access Java objects in a Java Virtual Machine. Methods are called as if the Java objects resided in the Python interpreter and Java collections can be accessed through standard Python collection methods. Py4J also enables Java programs to call back Python objects. Py4J is distributed under the BSD license.

Links

Pros

Cons

Good: py4j can be a good candidate to allows python interaction with a long-running java program/server.

How

py4j do not start the JVM by itself. The JVM needs to be started prior to the execution of the python code and creates an application gateway.

py4j: Java gateway implementation

import py4j.GatewayServer;

public class GatewayApplication {

  public int called_from_python(int first, int second) {
    return first + second;
  }

  public static void main(String[] args) {
    GatewayApplication app = new GatewayApplication();
    // app is now the gateway.entry_point
    GatewayServer server = new GatewayServer(app);
    server.start();
  }
}

The Python code will connect to this running application to interact with the JVM.

py4j: Python gateway

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)
gateway_app = gateway.entry_point         # get the GatewwayApplication instance
value = gateway_app.called_from_python(number1, number2) # call the addition method
print(value)

jpype

JPype is a cross-language bridge to allow python programs full access to java class libraries.

JPype is a Python module to provide full access to Java from within Python. Unlike Jython, JPype does not achieve this by re-implementing Python, but instead by interfacing both virtual machines at the native level. This shared memory-based approach achieves good computing performance while providing the access to the entirety of CPython and Java libraries. This approach allows direct memory access between the two machines, implementation of Java interfaces in Python, and even use of Java threading.

Links

Pros

Cons

Example

JPype: Using a .jar form Python

# Boiler plate stuff to start the module
import jpype
import jpype.imports
from jpype.types import *

# Launch the JVM
jpype.startJVM(classpath=['jars/database.jar'])

# import the Java modules
from com.paying.customer import Database

db = Database("our_records")
with db.connect() as c:
   c.runQuery()
   while c.hasRecords():
       recor...

pyjnius

A Python module to access Java classes as Python classes using the Java Native Interface (JNI).

Pros

Cons

Links

JCC

JCC is a C++ code generator that produces a C++ object interface wrapping a Java library via Java's Native Interface (JNI). JCC also generates C++ wrappers that conform to Python's C type system making the instances of Java classes directly available to a Python interpreter.

Pros

Cons

Links

python-javabridge

Python wrapper for the Java Native Interface

Links

Jython

Jython is a Java implementation of Python. It is not a library to call Java from Python but an implementation of Python in Java. As such, it can interface easily with Java but greatly limits the usefulness of the solution. Python 2.7 only

Links