I have a Java class library (3rd party, proprietary) and I want my python script to call its functions. I already have java code that uses this library. What is the best way to achieve this?
Py4J enables Python programs running in a Python interpreter to dynamically access Java objects in a Java Virtual Machine. Methods are called as if the Java objects resided in the Python interpreter and Java collections can be accessed through standard Python collection methods.
The seamless interaction between Python and Java allows developers to freely mix the two languages both during development and in shipping products.
To call a method in Java, simply write the method's name followed by two parentheses () and a semicolon(;).
Can you run your current Python scripts under Jython ? If so, that's probably the best way, since the Java library can be exposed directly into Jython as scriptable objects.
Failing that, there are a number of solutions listed here.
The other answer is JPype, which allows CPython to talk to Java. It's useful if you can't switch to Jython.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With