Pyjnius: Accessing Java classes from Python

We’re pleased to announce the Pyjnius project. It’s a Python library to access Java classes from Python, on the desktop or on Android.

We found others libraries such as JPype or Py4j, but we weren’t happy with the design and the usability. Using Jython wasn’t an option either, because we wanted to use the library within our python for android project.

Let me show you how simple it is to use Pyjnius:

>>> from jnius import autoclass
>>> Stack = autoclass('java.util.Stack')
>>> stack = Stack()
>>> stack.push('hello')
>>> stack.push('world')
>>> stack.pop()
>>> stack.pop()

What we demonstrate here is the autoclass function that create kind-of proxy that reflect all the methods and fields available from the Java class java.util.Stack.

Ok, maybe you want an Android-related example? Just have a look:

from jnius import autoclass
from time import sleep

MediaRecorder = autoclass('')
AudioSource = autoclass('$AudioSource')
OutputFormat = autoclass('$OutputFormat')
AudioEncoder = autoclass('$AudioEncoder')

# Record the Microphone with a 3GP recorder
mRecorder = MediaRecorder()

# Record 5 seconds

More examples are available in the documentation. We have a mapping between Java/Python type, native arrays, support for methods with multiple signatures, and a lot more. We are using Cython + JNI internally. The performance cost is minimal.

The library is already available for Python for android.

The library have been done by Gabriel Pettier and myself, under the Kivy organization.

13 thoughts on “Pyjnius: Accessing Java classes from Python

  1. @stu: sounds nice indeed, something like this ?

    from import Stack

    that could probably be done with some hacking in the import system, yeah 🙂

  2. Paul

    Can you elaborate (maybe in another post) on the problems you had with the alternatives like JPype?

    The documentation for pyjnius is also really scarce. For example, it doesn’t mention how to initialise the JVM by specifying the path to the relevant library, or how to set JVM startup options, or even what the full feature set is. Can it be used to provide Python implementations of Java interfaces?

    One problem with JPype is that it’s got a huge overhead when moving large arrays from Java to Python, because it converts them to Python lists of numbers. It’d be cool if pyjnius could use numpy arrays instead.

  3. tito

    I’ve no real experience like you with JPype. It’s almost the same, but the design is not. For example, we don’t provide class for native Java type (float, double, short, int, long). The conversion is “implicit”, when the method is called, according to the method signature.

    We initialize the JVM as you would start Java from the command line. And then, CLASSPATH is read as usual for example. You can look how how the JVM is created.
    That said, you could open a issue in the bug tracker for supporting more options about how the JVM is started. I’m not a Java guy, so all this part is kind-of new 🙂

    About overhead when moving large arrays, we’ll have the same issues. We could use numpy, or C-array from python. I would like to say… you can open an issue for that one too 🙂 I’ll not go by default on numpy, specially because it’s not currently supported on Android.

  4. @tshirtman Yup, exactly that sort of thing.

    I’d quite like what someone on the reddit thread asked for too:

    “[–]pje 1 point 13 hours ago

    Any plans to implement Jython-style keyword argument initializers? e.g. Foo(bar=baz) -> Foo().setBar(baz)? Or property setting, i.e. = baz translating to a setBar() call under the hood?

    How about idiomatic translation of Python iteration and item access to common Java iteration and mapping/indexing interfaces?

    (Yeah, I know, I’m greedy.)”

  5. Chris Barker

    This looks very promising — nice work!

    The issue with big arrays of numbers is a big one for many of us. One option to consider is the PEP-3118 buffers — It is supported by numpy, and I
    m not sure what else, but using it would not require the user to have numpy.

    It looks like you’re using Cython, and it support PEP-3118 buffer in various ways — maybe memoryviews would be good?

    What I”m not sure of is if the standanrd libary has anyting that impliments the full buffer protocol at all. But if not, you could at least use bytes or array.arrays, to get something efficient.

    -just some thoughts….

  6. Nam Nguyen

    This could be a really exciting talk at PyCon 2013. Why don’t you submit a proposal? It would be especially true when you complete the import hook.

  7. Luis Fernandes


    I’m trying to load/run my own java class with pyjnius. On ubuntu every thing is ok, Pyjnius find my own class, but if i run from mac 10.9 he complains with class not found. (jnius.JavaException: Class not found ‘org/saguas/report/MyDbReportFill’)

    I try every possible way. Set classpath on code before “from jnius import autoclass”, on environment (.bash_profile) but nothing woks. Like i said on ubuntu every thing woks fine with the same procedures.

    Do you have any suggestions?

    Luís Fernandes.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.