Skip to content

Instantly share code, notes, and snippets.

@bartdag
Created July 7, 2011 19:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save bartdag/1070311 to your computer and use it in GitHub Desktop.
Save bartdag/1070311 to your computer and use it in GitHub Desktop.
Java and Python and Py4J
package p1;
import py4j.GatewayServer;
public class MyApplication {
public static void main(String[] args) {
GatewayServer server = new GatewayServer(null);
// This will start the Py4J server and now, the JVM is ready to receive Python commands.
// Once gateway.shutdown is called on the Python side, this call will return, and the Java program
// will exit.
// Obviously, this can be way more complex, but it's a good start :-)
server.start();
}
}
import subprocess
import py4j.java_gateway import JavaGateway
def start_java():
# Note: I assume that my_library.jar contains the library you want to expose with your CLI program.
ARGS = ['java', '-cp', '/path/to/my_library.jar:/path/to/my/class_dir:/path/to/py4j-java.jar', 'p1.MyApplication']
p = subprocess.Popen(ARGS)
print('Java Started: {0}'.format(p.pid))
def stop_java():
gateway = JavaGateway()
gateway.shutdown()
# Two alternatives:
# (1) You could call a method on the Java side that calls System.exit(0);
# (2) You could save the pid from start_java and kill the process, but you need to handle mac, linux, and
# windows...
@fredlumm
Copy link

Hi,

Not sure if you can see it. I recently just came across the problem of starting python process from Java. And What i did was using runtime.getRuntime.exec() to launch a new python process to create the JavaGateway, and the rest of it would be same of tutorial on websites, which is just start the Gatewayserver in the following java code. At the very end I would destroy the python process. However, it throws an error:
py4j.Py4JException: Error while obtaining a new communication channel
at py4j.CallbackClient.getConnectionLock(CallbackClient.java:218)
at py4j.CallbackClient.sendCommand(CallbackClient.java:337)
at py4j.CallbackClient.sendCommand(CallbackClient.java:316)
at py4j.reflection.PythonProxyHandler.invoke(PythonProxyHandler.java:106)
at com.sun.proxy.$Proxy0.predict(Unknown Source)
at py4j.examples.ClientServer.main(ClientServer.java:31)
Caused by: java.net.ConnectException: Connection refused
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at java.net.Socket.connect(Socket.java:538)
at java.net.Socket.(Socket.java:434)
at java.net.Socket.(Socket.java:244)
at javax.net.DefaultSocketFactory.createSocket(SocketFactory.java:277)
at py4j.CallbackConnection.start(CallbackConnection.java:206)
at py4j.CallbackClient.getConnection(CallbackClient.java:199)
at py4j.CallbackClient.getConnectionLock(CallbackClient.java:211)

Are you able to help with this?

Best

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment