Send a Python object to Java using Py4j

左心房为你撑大大i 提交于 2019-12-04 05:56:31

the problem is that you cannot send pure Python objects to the Java side (in this case, calling push actually calls the Java method "Stack.push"). You can only send (1) objects that can be automatically converted to Java objects (primitives such as int, byte array, strings), (2) objects received from Java such as "stack", or (3) Python objects implementing a Java interface:

class Event(object):
    def myMethod(param1, param2):
        return "foo"

    class Java:
        implements = ['yourpackage.IEvent']

If you want to send Python objects implementing a Java interface, you need to accept incoming connections from the Python interpreter (the JVM will call back the Python interpreter if a Python method is called):

gateway = JavaGateway(start_callback_server=True)
stack = gateway.entry_point.getStack()
event = Event()
stack.push(event)
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!