Calling Java from Python without the JVM startup latency: NailGun and JPype

Even though Python is a great platform with tons of libraries available, there are still a whole universe of functionality still available only in the Java world, and so, you will sometimes need to bridge the worlds together.

The easiest solution to do this would be to do a simple subprocess call to invoke the java code via a system call. This does not need any external libraries at all. The downside is that the startup time of the Java Virtual Machine (JVM), which is at least around 100 milliseconds for a simple hello world type of program.

What do do then? Enter NailGun and JPype!

NailGun takes the approach of allowing you to run any existing Java class as is, just faster. It does so by starting a JVM server process in the background, and executing the desired Java code in this same started JVM process without stopping the JVM in between, and thus clears away the JVM startup time completely, after the first startup. The execution is done via a very thin compiled client, written i C.

JPype on the other hand, is a python-only solution, that works in a similar way like NailGun, that the JVM is started once, and subsequent calls to it are made to the already started JVM process, but it it tightly integrated in the python environment, providing access to your Java code from within python.

There's a few tutorials on NailGun here, here and here, and on JPype here, here and here, so I won't go into too much details on the usage, but just document my setup, and show some initial performance comparison I made.

TBC ...