This is a quick start guide to using JPype with Java. This guide will show a
series of snippets with the corresponding commands in both Java and Python for
using JPype. The JPype User Guide and API Reference have additional details on
the use of the JPype module.
JPype uses two factory classes (JArray and JClass) to produce class
wrappers which can be used to create all Java objects. These serve as both the
base class for the corresponding hierarchy and as the factory to produce new
wrappers. Casting operators are used to construct specify types of Java types
(JObject, JString, JBoolean, JByte, JChar, JShort,
JInt, JLong, JFloat, JDouble). Two special classes serve as the
base classes for exceptions (JException) and interfaces (JInterface).
There are a small number of support methods to help in controlling the JVM.
Lastly, there are a few annotations used to create customized wrappers.
For the purpose of this guide, we will assume that the following classes were
defined in Java. We will also assume the reader knows enough Java and Python
to be dangerous.
packageorg.pkg;publicclassBaseClass{publicvoidcallMember(inti){}}publicclassMyClassextendsBaseClass{finalpublicstaticintCONST_FIELD=1;publicstaticintstaticField=1;publicintmemberField=2;intinternalField=3;publicMyClass(){}publicMyClass(inti){}publicstaticvoidcallStatic(inti){}publicvoidcallMember(inti){}// Python name conflictpublicvoidpass(){}publicvoidthrowsException()throwsjava.lang.Exception{}// Overloaded methodspublicvoidcall(inti){}publicvoidcall(doubled){}}
The hardest thing about using JPype is getting the jars loaded into the JVM.
Java is curiously unfriendly about reporting problems when it is unable to find
a jar. Instead, it will be reported as an ImportError in Python.
These patterns will help debug problems with jar loading.
Once the JVM is started Java packages that are within a top level domain (TLD)
are exposed as Python modules allowing Java to be treated as part of Python.
Description
Java
Python
Start Java Virtual
Machine (JVM)
# Import moduleimportjpype# Enable Java importsimportjpype.imports# Pull in typesfromjpype.typesimport*# Launch the JVMjpype.startJVM()
Start Java Virtual
Machine (JVM) with a
classpath
# Launch the JVMjpype.startJVM(classpath=['jars/*'])
Java classes are presented wherever possible similar to Python classes. The
only major difference is that Java classes and objects are closed and cannot be
modified. As Java is strongly typed, casting operators are used to select
specific overloads when calling methods. Classes are either imported using a
module, loaded using JPackage or loaded with the JClass factory.
Java exceptions extend from Python exceptions and can be dealt with in the same
way as Python native exceptions. JException serves as the base class for all
Java exceptions.
Most Python primitives directly map into Java primitives. However, Python does
not have the same primitive types, and it is necessary to cast to a
specific Java primitive type whenever there are Java overloads that would
otherwise be in conflict. Each of the Java types are exposed in JPype
(JBoolean, JByte, JChar, JShort, JInt, JLong,
JFloat, JDouble).
Java strings are similar to Python strings. They are both immutable and
produce a new string when altered. Most operations can use Java strings
in place of Python strings, with minor exceptions as Python strings
are not completely duck typed. When comparing or using as dictionary keys,
all JString objects should be converted to Python.
Java reflection can be used to access operations that are outside the scope of
the JPype syntax. This includes calling a specific overload or even accessing
private methods and fields.
JPype can implement a Java interface by annotating a Python class. Each
method that is required must be implemented.
JPype does not support extending a class directly in Python. Where it is
necessary to exend a Java class, it is required to create a Java extension
with an interface for each methods that are to be accessed from Python.