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

I have a java library in jar form that can be used to extract data from files (excel, xml, etc.). Like it in java, it can only be used in Java applications. But I need the same library, which will also be used for python projects. I tried py4j etc. which takes objects from jvm. But the library is not an executable file and will not "run". I checked Jython, but I need a library to be accessible from Python projects. I was thinking about using automated java for python translators, but I would take this as a last resort.

Please suggest a way I can do this.

+4
source share
3 answers

You can create a one-class java program with a thread that never ends until you send a notification about this from Python.

This way lib will be stored in memory and accessible from your Python program.

This class may be like this (add the necessary import / init lib):

public class TestPy { private Thread thread; public void die() { synchronized (thread) { thread.interrupt(); } } public TestPy() { thread = new Thread(){ public void run() { try { while (!Thread.interrupted()) { Thread.sleep(500); } } catch (InterruptedException e) { Thread.currentThread().interrupt(); } } }; thread.start(); } public static void main(String[] args) { TestPy tp = new TestPy(); GatewayServer server = new GatewayServer(tp); server.start(); } } 

You will need to run the java program, use lib, and then use the die () method to destroy the java program in Python:

 gateway = JavaGateway() do your stuff here using the lib tp = gateway.entry_point tp.die() 
+2
source

You can write a simple Java command program that calls the library and saves the results in a format that you can read in Python, then you can invoke the program with Python using os.system .

Another option is to find Python libraries with equivalent functionality for the Java library: you can read excel, xml and other files in Python, this is not a problem.

+2
source

I did not learn how to create new instances of a java class in a jar file by java constructors, but accidentally discovered that it is very easy to use any static java methods to access Java objects in py2j.

step 1: download the py4j zip file from https://pypi.python.org/pypi/py4j . "py4j0.10.0.jar" is located in the zip file.

Step 2: install py4j with

 pip install 'D:\Downloads\py4j-0.10.0.zip' 

Step 3: add py4j0.10.0.jar as well as the_lib_you_use.jar (e.g. owlapi-distribution-3.5.0.jar for the example below) to create a path in the eclipse project

Step 4: create AdditionApplication.java and copy and paste the AdditionApplication.java code into https://www.py4j.org/ and run the Java application AdditionApplication.java

Step 5: after running AdditoinApplication.java check the sample code in the python file:

 if __name__ == '__main__': pass from py4j.java_gateway import JavaGateway gateway = JavaGateway() # connect to the JVM random = gateway.jvm.java.util.Random() # create a java.util.Random instance number1 = random.nextInt(10) # call the Random.nextInt method number2 = random.nextInt(10) print(number1,number2) (2, 7) addition_app = gateway.entry_point # get the AdditionApplication instance addition_app.addition(number1,number2) # call the addition method Math = gateway.jvm.java.lang.Math a = Math.max(4, 6); print a IRI = gateway.jvm.org.semanticweb.owlapi.model.IRI abcIRI = IRI.create('fewf#fe') print 'abcIRi = ' + str(abcIRI) print 'abcIRI.getFragment() = ' + abcIRI.getFragment() 

Result on the console:

 (5, 0) 6 abcIRi = fewf#fe abcIRI.getFragment() = fe 
0
source

All Articles