py4j.Py4JException: Method testConnection([]) does not exist

147 views
Skip to first unread message

Cameron Meek

unread,
May 5, 2023, 11:10:34 AM5/5/23
to Py4J Support and Comments

Hello,

I am trying to get py4j working and am getting the error "py4j.Py4JException: Method testConnection([]) does not exist" when I try to call a simple test function. Interestingly I can call "gateway.entry_point.getClass().getName()" and it will tell me the correct class name. I can also call "gateway.entry_point.getClass().getDeclaredMethods()" and I can see that the method does exist. But when I try to call the method using "ateway.entry_point.testConnection()" I get the "does not exist" error.

This is using version 0.10.9.7 and I have tried it on two different computers, one Mac and one Windows.

My test code and the full error is below. Any idea why this is happening?

Thanks
Cameron


Here is the main class:

package org.testPy4J;

import py4j.GatewayServer;

public class testPy4J {

public static void main(String[] args) {
            GatewayServer gatewayServer = new GatewayServer(new testGateway());
            gatewayServer.start();
            System.out.println("Gateway Server Started");
}

}


Here is the test gateway class:

package org.testPy4J;

public class testGateway {
public void testConnection() {
            System.out.println("Connection Works");
}
}


Here is the python I am executing and the error:
>>> from py4j.java_gateway import JavaGateway
>>> gateway = JavaGateway()
>>> gateway.entry_point.testConnection()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/Cameron/opt/anaconda3/lib/python3.8/site-packages/py4j/java_gateway.py", line 1322, in __call__
    return_value = get_return_value(
  File "/Users/Cameron/opt/anaconda3/lib/python3.8/site-packages/py4j/protocol.py", line 330, in get_return_value
    raise Py4JError(
py4j.protocol.Py4JError: An error occurred while calling t.testConnection. Trace:
py4j.Py4JException: Method testConnection([]) does not exist
at py...@0.10.9.7/py4j.reflection.ReflectionEngine.getMethod(ReflectionEngine.java:321)
at py...@0.10.9.7/py4j.reflection.ReflectionEngine.getMethod(ReflectionEngine.java:329)
at py...@0.10.9.7/py4j.Gateway.invoke(Gateway.java:274)
at py...@0.10.9.7/py4j.commands.AbstractCommand.invokeMethod(AbstractCommand.java:132)
at py...@0.10.9.7/py4j.commands.CallCommand.execute(CallCommand.java:79)
at py...@0.10.9.7/py4j.GatewayConnection.run(GatewayConnection.java:238)
at java.base/java.lang.Thread.run(Thread.java:834)

Cameron Meek

unread,
May 5, 2023, 2:40:16 PM5/5/23
to Py4J Support and Comments, Cameron Meek
I resolved my issue. I needed to add a "exports" declaration in the module-info file.
Reply all
Reply to author
Forward
0 new messages