axatrikx axatrikx - 8 months ago 57
Python Question

Use java library from python (Python wrapper for java library)

I have a java library in jar form which can be used to extract data from files(excel,xml etc). As its in java, it can be used only in java applications. But i need the same library to be used for python projects as well.
I have tried py4j etc which takes the objects from jvm. But the library is not an executable and wont be 'run'. I have checked Jython but i need the library to be accessible from Python projects.
I have thought about using automated java to python translators, but i would take that as the last resort.

Please suggest some way i can accomplish this.


You can make a one class java program with a thread never ending until you send from Python a notification to do so.

This way the lib would be kept in memory and accessible from your Python program.

This class could be like this (add your needed lib import/init) :

public class TestPy {

    private Thread thread;

    public void die() {
        synchronized (thread) {

    public TestPy() {
        thread = new Thread(){
            public void run() {
                try {
                    while (!Thread.interrupted()) {
                } catch (InterruptedException e) {

    public static void main(String[] args) {
        TestPy tp = new TestPy();
        GatewayServer server = new GatewayServer(tp);

You would have to launch the java program, use the lib, and then use the die() method to kill the java program in Python :

gateway = JavaGateway()
do your stuff here using the lib
tp = gateway.entry_point