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 userguide
and api
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.
package org.pkg;
public class BaseClass
{
public void callMember(int i)
{}
}
public class MyClass extends BaseClass
{
final public static int CONST_FIELD = 1;
public static int staticField = 1;
public int memberField = 2;
int internalField =3;
public MyClass() {}
public MyClass(int i) {}
public static void callStatic(int i) {}
public void callMember(int i) {}
// Python name conflict
public void pass() {}
public void throwsException() throws java.lang.Exception {}
// Overloaded methods
public void call(int i) {}
public void call(double d) {}
}
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) | .. code-block:: python
|
|
Start Java Virtual Machine (JVM) with a classpath | .. code-block:: python
|
|
Import default Java namespace | .. code-block:: python
|
|
Add a set of jars from a directory | .. code-block:: python
|
|
Add a specific jar to the classpath | .. code-block:: python
|
|
Print JVM CLASSPATH | .. code-block:: python
|
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.
Description | Java | Python |
---|---|---|
Import a class | .. code-block:: java
|
.. code-block:: python
|
Import a class and rename | .. code-block:: python
|
|
Import multiple classes from a package | .. code-block:: python
|
|
Import a java package for long name access | .. code-block:: python
|
|
Import a class static | .. code-block:: java
|
.. code-block:: python
|
Import a class without tld | .. code-block:: java
|
.. code-block:: python
|
Construct an object | .. code-block:: java
|
.. code-block:: python
|
Constructing a class with full class name | .. code-block:: python
|
|
Get a static field | .. code-block:: java
|
.. code-block:: python
|
Get a member field | .. code-block:: java
|
.. code-block:: python
|
Set a static field | .. code-block:: java
|
.. code-block:: python
|
Set a member field | .. code-block:: java
|
.. code-block:: python
|
Call a static method | .. code-block:: java
|
.. code-block:: python
|
Call a member method | .. code-block:: java
|
.. code-block:: python
|
Access member with Python naming conflict | .. code-block:: java
|
.. code-block:: python |
Checking inheritance | .. code-block:: java
|
.. code-block:: python
|
Checking if Java class wrapper | .. code-block:: python
|
|
Checking if Java object wrapper | .. code-block:: python
|
|
Casting to a specific type | .. code-block:: java
|
.. code-block:: python
|
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.
Description | Java | Python |
---|---|---|
Catch an exception | .. code-block:: java
|
.. code-block:: python
|
Throw an exception to Java | .. code-block:: java
|
.. code-block:: python
|
Checking if Java exception wrapper | .. code-block:: python
|
|
Closeable items | .. code-block:: java
|
.. code-block:: python
|
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
).
Description | Java | Python |
---|---|---|
Casting to hit an overload | .. code-block:: java
|
.. code-block:: python
|
Create a primitive array | .. code-block:: java
|
.. code-block:: python
|
Create a rectangular primitive array | .. code-block:: java
|
.. code-block:: python
|
Create an array of arrays | .. code-block:: java
|
.. code-block:: python
|
Create an initialized primitive array | .. code-block:: java
|
.. code-block:: python
|
Create an initialized boxed array | .. code-block:: java
|
.. code-block:: python
|
Put a specific primitive type on a list | .. code-block:: java
|
.. code-block:: python
|
Boxing a primitive | .. code-block:: java
|
.. code-block:: python
|
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.
Description | Java | Python |
---|---|---|
Create a Java string | .. code-block:: java
|
.. code-block:: python
|
Create a Java string from bytes | .. code-block:: java
|
.. code-block:: python
|
Converting Java string | .. code-block:: python
|
|
Comparing Python and Java strings | .. code-block:: python
|
|
Comparing Java strings | .. code-block:: java
|
.. code-block:: python
|
Checking if Java string | .. code-block:: python
|
Arrays are create using the JArray class factory. They operate like Python lists, but they are fixed in size.
Description | Java | Python |
---|---|---|
Create a single dimension array | .. code-block:: java
|
.. code-block:: python
|
Create a multi dimension array (old) | .. code-block:: java
|
.. code-block:: python
|
Create a multi dimension array (new) | .. code-block:: java
|
.. code-block:: python
|
Access an element | .. code-block:: java
|
.. code-block:: python
|
Size of an array | .. code-block:: java
|
.. code-block:: python
|
Get last element | .. code-block:: java
|
.. code-block:: python
|
Slice an array | .. code-block:: python
|
|
Clone an array | .. code-block:: java
|
.. code-block:: python
|
Convert to Python list | .. code-block:: python
|
|
Iterate elements | .. code-block:: java
|
.. code-block:: python
|
Checking if java array wrapper | .. code-block:: python
|
Java standard containers are available and are overloaded with Python syntax where possible to operate in a similar fashion to Python objects.
Description | Java | Python |
---|---|---|
Import list type | .. code-block:: java
|
.. code-block:: python
|
Construct a list | .. code-block:: java
|
.. code-block:: python
|
Get length of list | .. code-block:: java
|
.. code-block:: python
|
Get list item | .. code-block:: java
|
.. code-block:: python
|
Set list item | .. code-block:: java
|
.. code-block:: python
|
Iterate list elements | .. code-block:: java
|
.. code-block:: python
|
Import map type | .. code-block:: java
|
.. code-block:: python
|
Construct a map | .. code-block:: java
|
.. code-block:: python
|
Get length of map | .. code-block:: java
|
.. code-block:: python
|
Get map item | .. code-block:: java
|
.. code-block:: python
|
Set map item | .. code-block:: java
|
.. code-block:: python
|
Iterate map entries | .. code-block:: java
|
.. code-block:: 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.
Description | Java | Python |
---|---|---|
Access Java reflection class | .. code-block:: java
|
.. code-block:: python |
Access a private field by name | .. code-block:: python
|
|
Accessing a specific overload | .. code-block:: python
|
|
Convert a java.lang.Class into Python wrapper |
.. code-block:: python
|
|
Load a class with a external class loader | .. code-block:: java
|
.. code-block:: python
|
Accessing base method implementation | .. code-block:: python
|
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.
Description | Java | Python |
---|---|---|
Implement an interface | .. code-block:: java
|
.. code-block:: python
|
Extending classes | None | |
Lambdas | .. code-block:: java
|
.. code-block:: python
|
Don't like the formatting? Feel the guide is missing something? Submit a pull request at the project page.