py4j.protocol.Py4JNetworkError: An error occurred while trying to connect to the Java server

2,572 views
Skip to first unread message

Petr Jezek

unread,
Jul 19, 2017, 8:18:08 PM7/19/17
to Py4J Support and Comments
Hi 

When I'm connecting to a remote host I'm getting following error: py4j.protocol.Py4JNetworkError: An error occurred while trying to connect to the Java server (xx.xx.xx.xx:25333)) (where xx.xx.xx.xx is the IP address I don't want to share). When I'm running gateway server on a localhost I'm able to connect without issues. In both cases I'm using default ports without any changing.

I'm fairly sure that firewall or something like that is not a problem because the host is a common ubuntu installation wihtout any IP tables rules configured.

Thanks
Petr

Barthelemy Dagenais

unread,
Jul 20, 2017, 4:37:22 AM7/20/17
to Petr Jezek, Py4J Support and Comments
Hi,

can you post your Java code showing how you initialize the
GatewayServer (Java) and the JavaGateway
(Py4J)?
> --
> 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 post to this group, send email to py...@py4j.org.
> To view this discussion on the web visit
> https://groups.google.com/a/py4j.org/d/msgid/py4j/4c033a2a-192c-45cd-936d-188624ea1604%40py4j.org.

Barthelemy Dagenais

unread,
Jul 20, 2017, 4:37:52 AM7/20/17
to Petr Jezek, Py4J Support and Comments
err, I meant JavaGateway (Python)

Petr Jezek

unread,
Jul 20, 2017, 1:34:24 PM7/20/17
to Py4J Support and Comments
Sure.

>>> from py4j.java_gateway import JavaGateway
>>> from py4j.java_gateway import GatewayParameters
>>> gateway = JavaGateway(gateway_parameters=GatewayParameters(address='<IP>'))
>>> res = gateway.executeQuery()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/petr-jezek/.local/lib/python2.7/site-packages/py4j/java_gateway.py", line 1158, in __call__
    answer = self.gateway_client.send_command(command)
  File "/home/petr-jezek/.local/lib/python2.7/site-packages/py4j/java_gateway.py", line 906, in send_command
    connection = self._get_connection()
  File "/home/petr-jezek/.local/lib/python2.7/site-packages/py4j/java_gateway.py", line 854, in _get_connection
    connection = self._create_connection()
  File "/home/petr-jezek/.local/lib/python2.7/site-packages/py4j/java_gateway.py", line 860, in _create_connection
    connection.start()
  File "/home/petr-jezek/.local/lib/python2.7/site-packages/py4j/java_gateway.py", line 997, in start
    raise Py4JNetworkError(msg, e)
py4j.protocol.Py4JNetworkError: An error occurred while trying to connect to the Java server (<IP>:25333)
>>>

Where <IP> is the IP of the server

And the server has been run like:
GatewayServer gatewayServer = new GatewayServer(facade);
gatewayServer.start();

where facade is an interface with the executeQuery method.

And as I said, where java server and python client are run both from localhost it works.

Petr


Dne čtvrtek 20. července 2017 1:37:52 UTC-7 Barthelemy Dagenais napsal(a):

Petr Jezek

unread,
Jul 20, 2017, 1:41:31 PM7/20/17
to Py4J Support and Comments


Dne středa 19. července 2017 17:18:08 UTC-7 Petr Jezek napsal(a):

Petr Jezek

unread,
Jul 20, 2017, 1:42:20 PM7/20/17
to Py4J Support and Comments


Dne středa 19. července 2017 17:18:08 UTC-7 Petr Jezek napsal(a):
Hi 

Petr Jezek

unread,
Jul 20, 2017, 1:42:44 PM7/20/17
to Py4J Support and Comments


Dne středa 19. července 2017 17:18:08 UTC-7 Petr Jezek napsal(a):
Hi 

Barthelemy Dagenais

unread,
Jul 20, 2017, 2:41:25 PM7/20/17
to Petr Jezek, Py4J Support and Comments
The GatewayServer only listens to localhost by default. You probably
want to call this constructor instead:
https://www.py4j.org/_static/javadoc/py4j/GatewayServer.html#GatewayServer-java.lang.Object-int-int-java.net.InetAddress-java.net.InetAddress-int-int-java.util.List-

The GatewayServer class also defines constants for the default values
of most parameters (e.g., default java port, default python port) so
you can use these constants to fill the various parameters that you do
not wish to override.

HTH
> https://groups.google.com/a/py4j.org/d/msgid/py4j/36562a70-6fbd-401d-aabd-cfa899446ee4%40py4j.org.

Petr Jezek

unread,
Jul 20, 2017, 6:51:57 PM7/20/17
to Py4J Support and Comments
Thanks, 

It helped. If I used the different constructor, the python code starts working.

Petr

Dne čtvrtek 20. července 2017 11:41:25 UTC-7 Barthelemy Dagenais napsal(a):
Reply all
Reply to author
Forward
Message has been deleted
0 new messages