Skip to main content
Version: Python

How to use jpy

jpy is a bi-directional Java-Python bridge that facilitates calling Java from Python and vice versa.

  • Python programs that use jpy can access Java functionalities.
  • Java programs that use jpy can access Python functionalities.

For more details on jpy, see the jpy GitHub project.

The Deephaven query engine is implemented in Java. As a result, it is relatively easy to use Java from within Python. jpy is used as the bridge between the two languages. This guide will cover how to use Java from Python. Calling Python from Java is much less common, and won't be covered here.

Java Types

Java types can be accessed from Python by calling jpy.get_type.

This first example gets java.lang.Runtime and uses it to print the total memory available to the JVM.

import jpy

Runtime = jpy.get_type("java.lang.Runtime")

rt = Runtime.getRuntime()

print(rt.totalMemory())

This second example gets java.util.Random and uses it to print a random integer and double value.

import jpy

Random = jpy.get_type("java.util.Random")
rng = Random()
new_random_int = rng.nextInt(100)
new_random_double = rng.nextDouble()

print(new_random_int)
print(new_random_double)

Java Arrays

Arrays of Java objects and primitives can be created in Python using jpy.array. Arrays can be created in two ways:

  • With a list of objects of the given type.
  • With an integer size for the array length.
import jpy

array1 = jpy.array("java.lang.String", ["Using", "jpy", "in", "Deephaven"])
array2 = jpy.array("double", 100)
array3 = jpy.array("char", 999)

More information

Python's help function can be used to get more information on jpy:

help("jpy")

Python's help function can also be used to get more information on Java types imported via jpy:

import jpy

JavaRandom = jpy.get_type("java.util.Random")

rng = JavaRandom()

help(rng)