Calling Unconfigured Java Code from Python

I want to be able to call certain methods and those that are contained in a Java bank that is already running (it is guaranteed that it will be running). I found things like Jython, but they seem to be able to access native Java classes and the like.

+6
source share
1 answer

See: Calling Java from Python

" You can also use Py4J. The first page has an example and a lot of documentation, but essentially you just call Java methods from your Python code, as if they were python methods:

from py4j.java_gateway import JavaGateway 

gateway = JavaGateway () # connect to the JVM

java_object = gateway.jvm.mypackage.MyClass () # call constructor

other_object = java_object.doThat ()

other_object.doThis (1, 'ABC')

gateway.jvm.java.lang.System.out.println ('Hello World!') # call a static method

Unlike Jython, one part of Py4J works in the Python VM, so it is always "updated" with the latest version of Python, and you can use libraries that do not work on Jython (for example, lxml). The other part runs on the Java virtual machine that you want to invoke.

Communication is via sockets instead of JNI, and Py4J has its own protocol (to optimize certain cases, to manage memory, etc.) "

+3
source

Source: https://habr.com/ru/post/918244/


All Articles