Change the future

Saturday 5:10 p.m.–5:40 p.m.

Integrating Jython with Java

Jim Baker, Shashank Bharadwaj

Audience level:
Core Python (Language, Stdlib)


Jython provides a nearly seamless integration when using Java code. Using Jython from Java is not nearly as seamless, at least not yet. There are also several choices, depending on what you're doing, from object factories to using Java Scripting (JSR-223) support. This talk will discuss techniques, gotchas, and ongoing work to improve integration.


We like to describe Jython as simply being Python for the Java platform. It's important to know that Jython is a highly compliant implementation of Python, as seen by testing with the standard library test suite (regrtest). Currently, Jython 2.7 is under development, which is compatible with Python 2.7 and its stdlib. What Jython uniquely provides then is a high level of integration with this underlying Java platform.

In Jython, Python code can directly import Java classes and work with Java objects. Your programs can call Java methods, or have Java call back into Python. If the Java method takes a single-method interface, you can even directly pass in a Python function, including a closure. It's even possible to get and set private fields (a capability given by reflection). Finally, a Python class can readily extend a Java class and/or interfaces. However, there are some limitations imposed by working with the Java object model, as we will discuss, along with performance considerations, whether that is the overhead of reflection or the cost of converting between Python and Java representations.

It's a bit more involved for Java code to directly use Python code. (There's no way just yet to say "import django;" into a Java project.) We will next talk about the APIs Jython provides for this integration. In particular, we will discuss:

  • JSR-223. This Java JSR introduced the Java Scripting API, which Jython fully supports. Although the integration is intentionally shallow, it's still a very usable integration point.

  • Object factories. Such factories use Jython-specific APIs to efficiently build Python objects. With some more wrapping, they can also work around the lack of Java annotation support.

Advanced mechanisms include:

  • Controlling generated proxies. This functionality enables such specific support as copy.copy support of Java objects, or iterating over them.

  • Exposing Java classes as Python types for better performance. You can make your Java class look and behave like a Python type, just by using the same mechanisms the Jython runtime does. This exposure also reduces the overhead of the Java integration, by removing boxing/unboxing overhead.

  • Using Jython's public API. This is also part of object factory support, but it's possible to use in a variety of ways, mMuch like the Python C extension API.

We will also discuss some work in progress:

  • Gradual typing. Python is dynamically typed; Java is statically typed. Some of the seams in the Java-Jython integration is seen in this different approach to typing. Gradual typing is a scheme to bridge the two. In particular, we are using gradual typing to enable - in the future! - more direct importing of Python code into Java itself.

  • Blame tracking. Building on gradual typing, blame tracking enhances the tracebacks we commonly use in debugging so that we can identify the root cause of type conflicts. This can simplify debugging in mixed Java/Python code.