I'm running a basic py4j program as follows:
from py4j.java_gateway import JavaGateway
gateway = JavaGateway.launch_gateway()
example = gateway.entry_point
results = example.foo()
print(results) // "foo"
Originally, my java program looked like this
package foo;
import py4j.GatewayServer;
public class Example {
public static void main(String[] args) throws IOException {
GatewayServer gatewayServer = new GatewayServer(new Example());
gatewayServer.start();
System.out.println("PyJ Gateway Server Started");
}
public String foo() {
return "foo";
}
}
Environment details: I'm running the python program via asdf+poetry python and building the java jar with maven. Python version 3.9.17, Java version 17.
After building and running my server a few times, I realized I needed a new method, so got rid of foo(), replacing it with bar(). So now my program looks like the following:
package foo;
import py4j.GatewayServer;
public class Example {
public static void main(String[] args) throws IOException {
GatewayServer gatewayServer = new GatewayServer(new Example());
gatewayServer.start();
System.out.println("PyJ Gateway Server Started");
}
public String bar(String input) {
return input;
}
}
However, now after rebuilding and relaunching my java jar for Example, I'm seeing the following results in my python program:
from py4j.java_gateway import JavaGateway
gateway = JavaGateway.launch_gateway()
example = gateway.entry_point
results = example.foo()
print(results) // "foo"
results = example.bar("bar") // py4j.protocol.Py4JError: An error occurred while calling t.bar. Trace: py4j.Py4JException: Target Object ID does not exist for this gateway :t
Note that these results persist, even if I locally kill all processes with "java" in the name, delete the original JAR, or anything else. Somehow py4j seems to have learned the first ever JAR I presented it with and will never let go.
How do I kill the deprecated java code and prevent python from speaking to it ever again?
It turns out that using
JavaGateway.launch_gateway()was restarting some cached version of my java jar. Rather than using the genericlaunch_gateway, using a more specificJavaGatewayconstructor worked.