Accessing Java Classes in Python Using Pyjnius | by Eldad Uzman | Mar, 2022

Arrange an superior Python to Java bridge

Laptop on desk
Photograph by AltumCode on Unsplash.

Bridging means utilizing behaviors and datatypes of 1 runtime setting in a unique runtime setting.

In different phrases, utilizing lessons, strategies, capabilities, and knowledge fields initially written in a single language (say, Java) in a unique language (say, Python).

In our instance, we are going to create a easy code in Java, after which we are going to write a Python program that consumes the Java code into its runtime.

Ideally, we wish to have every thing in only one programming language, however ideally suited isn’t all the time accessible.

Many occasions, we might discover a terrific resolution for an issue that’s not written in our language of selection.

One resolution for that could possibly be to deploy a server that can ‘serve’ these implementations with API calls.

However sustaining a server is likely to be expensive.

Not solely would that require growing a server-side utility and exposing the wanted behaviors and APIs, however this server must be hosted and deployed and that comes at a price.

Furthermore, if the required habits holds some inner state, then the server would now have to take care of that state for each explicit shopper. This could possibly be extraordinarily buggy and unstable.

So, bridging these behaviors domestically could be a greater various in some conditions.

Right here’s a easy Java utility:

This class has two strategies: one calculates a easy common of an inventory of numbers, and the opposite is a stateful technique.

The state is the issue that will get incremented each time calculateFactoredMean is named, and the imply is multiplied by the issue.

So, a typical essential app could be one thing like the next:

Very simple. The principle technique instantiates the Calculator class with an preliminary issue being 1 and a step increment of two, after which the calculateFactoredMean is named 3 times.

The output of this utility could be:

Imply of checklist is: 5.50
Imply of checklist is: 16.50
Imply of checklist is: 27.50

Now, let’s devour this code right into a Python utility.

Pyjnius relies on the Java native interface and reflection to provision Java lessons into Python runtime.


  • you could have Java put in. In my case, I used openJDK 11.
  • You additionally want Javac put in (for instance, apt-get set up openjdk-11-jdk-headless).

Set up pyjinus

pip set up pyjnius==1.4.1

Hey World

This code generates a reference to Java System class after which it invokes the out.println static technique with the enter ‘Hey world.`This could print ‘Hey world’ to the usual output.

Let’s run this utilizing the next command:


Right here’s the output:

Hey world


Costumed libraries

To make use of our costumed libraries, we first have to compile our Java code right into a Jar file.

That is simply completed with maven, which will be put in utilizing this command:

mvn clear set up

Jar can be positioned within the goal listing.

Now, in our Python code, we must always add the jar file to our Java classpath as proven under:

First, we concluded the place the jar file is at, then we added it to the Java classpath utilizing jnius_config.set_classpath.

Now our essential part does the very same factor our essential technique in Java did.

It instantiated an inventory of integers from 1 to 10.

Word: this can be a Java checklist; the Java class can’t settle for Python datatypes!

Then it will get a reference to the Calculator class after which it instantiates an object out of it. Bear in mind, lessons in Python are additionally objects!

Then it consumes the calculateFactoredMean habits, calling the strategy on the checklist 3 times.

Let’s run this utilizing the next command:

python some-python-app

Right here’s the output:

Imply of checklist is: 5.5
Imply of checklist is: 16.5
Imply of checklist is: 27.5


Bridging between languages may contain interprocess communication that is likely to be expensive for some purposes.

It requires good data of each the native language and the bridged language.

Establishing the undertaking is likely to be tough and imposing obstacles to entry and operating an utility with bridges may enhance dependencies that may make it too tough to execute.

More Posts