I am using Java callback functions in Python using Py4J.
Java openjdk 11.0.3
Python 3
Py4J 0.10.8.1
I have successfully used Py4J in the past to use Java callback functions in Python. However, I recently added a new callback class using the same pattern I used before, but now I get the following error with the new class.
AttributeError: 'JavaMember' object has no attribute '_get_object_id'
In my attempts to track down the issue, I printed out the Java objects in
Python. The old class StatusCallback shows an object type of TestsJNI$StatusCallback@548ebebc, but the new class shows an object type of <py4j.java_gateway.JavaMember object at 0x7f8dc69817f0>.
This explains the AttributeError I got. But I can't figure out why I am not getting py4j.java_gateway.JavaMember instead of something like TestsJNI$UploadCallback.
import py4j.GatewayServer;
public class TestsJNI
{
public static boolean functionCalled = false;
public static class UploadCallback
{
public static void callback()
{
functionCalled = true;
}
// I have also tried these functions
//public static void callback(int a)
//{
// functionCalled = true;
//}
//public static void callback(int a, boolean b)
//{
// functionCalled = true;
//}
}
public static UploadCallback uploadCallback = new UploadCallback();
public static class StatusCallback
{
public static void callback()
{
functionCalled = true;
}
}
public static StatusCallback statusCallback = new StatusCallback();
public static void main(String args[])
{
TestsJNI testApp = new TestsJNI();
// Py4J server
GatewayServer server = new GatewayServer(testApp);
server.turnLoggingOff();
server.start();
}
}
from py4j.java_gateway import JavaGateway, GatewayParameters, get_field
javaCmd = ("java -cp /mnt/c/Workspace/tests/java/:.:/home/fred/.local/share/py4j/py4j0.10.8.1.jar TestsJNI")
print(javaCmd)
self.jvmProcess = Popen(javaCmd, shell=True, preexec_fn=os.setsid)
time.sleep(1.0)
# Connect to JVM via Py4J gateway
logging.getLogger("py4j").setLevel(logging.ERROR)
self.gateway = JavaGateway(gateway_parameters=GatewayParameters(auto_field=True))
entryPoint = self.gateway.entry_point
self.jni = entryPoint.autoguidanceJNI
self.jniObj = self.gateway.entry_point
testObj = self.jniObj.uploadCallback
print("upload testObj = " + str(testObj))
ret = False
testObj = self.jniObj.StatusCallback
print("status testObj = " + str(testObj));
upload testObj = <py4j.java_gateway.JavaMember object at 0x7f8dc69817f0>
status testObj = TestsJNI$StatusCallback@548ebebc
Why does Py4J give me a py4j.java_gateway.JavaMember object for the UploadCallback class and a TestsJNI$StatusCallback object for the StatusCallback class?
--
You received this message because you are subscribed to the Google Groups "Py4J Support and Comments" group.
To unsubscribe from this group and stop receiving emails from it, send an email to py4j+uns...@py4j.org.
To view this discussion on the web visit https://groups.google.com/a/py4j.org/d/msgid/py4j/be9c05bf-a0b2-4b33-8316-41d7e10d1799%40py4j.org.
public class ExampleClass {
public static class UploadCallback {
public static void callback() {
System.out.println("Callback called");
}
}
public static UploadCallback uploadCallback = new UploadCallback();
--
You received this message because you are subscribed to the Google Groups "Py4J Support and Comments" group.
To unsubscribe from this group and stop receiving emails from it, send an email to py4j+uns...@py4j.org.
To view this discussion on the web visit https://groups.google.com/a/py4j.org/d/msgid/py4j/6f4b96e5-4782-42fc-973b-bc14fdc6f688%40py4j.org.
To view this discussion on the web visit https://groups.google.com/a/py4j.org/d/msgid/py4j/CAM_%3DWAad8E4Gr8ru7zzRsgwqzq8i507xZdbzUhUe%3DtXx4NOg%3Dw%40mail.gmail.com.