from python, call a java method from a server on a different ip address

1,019 views
Skip to first unread message

Jim Hicks

unread,
Feb 9, 2016, 7:05:26 PM2/9/16
to Py4J Support and Comments
Hi,

I am running python 3.4 in interactive mode.  I have been successful calling the simple AdditionApplication.addition java method (on the py4j.org home page) when the java GatewayServer is running on the same machine as my python session.  I have not been able to call the method though when the java GatewayServer is running on a different computer.

I tried the following from python when the java program is running on 192.168.1.167 (different from the ip address where the python shell is running), with the resulting error shown:

>>> from py4j.java_gateway import JavaGateway, GatewayParameters
>>> gateway = JavaGateway(GatewayParameters(address=u'192.168.1.167')) 
>>> my_app = gateway.entrypoint
>>> value = my_app.addition(1,3)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'JavaMember' object has no attribute 'addition'


It appears my_app is not really being associated with the java process running on the remote computer, even though no error messages are generated.  Is it possible to do what I'm trying to do?  Can anyone suggest how to specify the location of the server for the java methods I want to call?

Thanks very much for any suggestions or follow up questions.

Barthelemy Dagenais

unread,
Feb 9, 2016, 7:46:33 PM2/9/16
to Jim Hicks, Py4J Support and Comments
Hi Jim,

It is certainly possible to connect to a JVM hosted on another computer.

In your code example, you should first replace gateway.entrypoint by gateway.entry_point (note the underscore).

If it still does not work, I'll need more info (e.g., commands used to start the JVM on the remote computer, if it's possible to ping the other computer, if there is a firewall, etc.).

Bart

--
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/75e93d6f-b280-46b8-be28-298af8543b8d%40py4j.org.

Jim Hicks

unread,
Feb 9, 2016, 8:37:05 PM2/9/16
to Py4J Support and Comments
Thanks for pointing out the typo (no "_").  I probably have an over complicated environment.  I started the java process using apache spark (v1.6.0) and started the python process using pyspark.  Py4J is available to both processes, and the simple addition application works fine when they are both on my localhost.  When the java process is on a remote host, with the correct syntax, I now get error messages as if no server were started:

Traceback (most recent call last):
  File "C:\Users\Jim\Documents\temp\spark-1.6.0-bin-hadoop2.6\python\lib\py4j-0.9-src.zip\py4j\java_gateway.py", line 57
7, in _get_connection
IndexError: pop from an empty deque

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\Jim\Documents\temp\spark-1.6.0-bin-hadoop2.6\python\lib\py4j-0.9-src.zip\py4j\java_gateway.py", line 69
0, in start
ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\Jim\Documents\temp\spark-1.6.0-bin-hadoop2.6\python\lib\py4j-0.9-src.zip\py4j\java_gateway.py", line 81
1, in __call__
  File "C:\Users\Jim\Documents\temp\spark-1.6.0-bin-hadoop2.6\python\lib\py4j-0.9-src.zip\py4j\java_gateway.py", line 62
4, in send_command
  File "C:\Users\Jim\Documents\temp\spark-1.6.0-bin-hadoop2.6\python\lib\py4j-0.9-src.zip\py4j\java_gateway.py", line 57
9, in _get_connection
  File "C:\Users\Jim\Documents\temp\spark-1.6.0-bin-hadoop2.6\python\lib\py4j-0.9-src.zip\py4j\java_gateway.py", line 58
5, in _create_connection
  File "C:\Users\Jim\Documents\temp\spark-1.6.0-bin-hadoop2.6\python\lib\py4j-0.9-src.zip\py4j\java_gateway.py", line 69
7, in start
py4j.protocol.Py4JNetworkError: An error occurred while trying to connect to the Java server


My remote java process is started by:

C:\Users\jim>%SPARK_HOME%\bin\spark-submit --class demo.SimpleServer --master spark://192.168.1.167:7077 w:\users\jim\workspace-mars\learningSpark\target\learningSpark-0.0.1-SNAPSHOT.jar

My local java process (works!) is started by:

C:\Users\Jim>%SPARK_HOME%\bin\spark-submit --class demo.SimpleServer --master local \users\jim\workspace-mars\learningSp
ark\target\learningSpark-0.0.1-SNAPSHOT.jar


The w: is mapped to my localhost, so the same jar file is referenced in both my trials.  Both machines are in the same domain with no firewall.  I can ping the other computer.  I can start a java client using spark and connect to the server on the remote computer.

Thank you for your help.  Tomorrow I'll try to remove spark and test a very basic implementation, although I do want to use spark eventually.

Thanks again!


Barthelemy Dagenais

unread,
Feb 10, 2016, 4:40:58 AM2/10/16
to Jim Hicks, Py4J Support and Comments
Hi Jim, I just noticed another small error in your code, you should replace

gateway = JavaGateway(GatewayParameters(address=u'192.168.1.167'))

with

gateway = JavaGateway(gateway_parameters=GatewayParameters(address=u'192.168.1.167'))

The first argument of the JavaGateway constructor is a GatewayClient (deprecated).

This is the third times someone does this small mistake: did you try this from the official documentation (in that case, I really need to fix it)?

Thanks,
Bart

--
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.

Jim Hicks

unread,
Feb 10, 2016, 12:17:27 PM2/10/16
to Barthelemy Dagenais, Py4J Support and Comments
Hi Bart,

Thank you again for correcting my syntax.  FYI, I searched, but found no example for specifying the host address for the java server, so I inferred it from the example at: https://www.py4j.org/getting_started.html#writing-the-java-program.  That's where I found the example without the gateway_parameters keyword.

After reading that web page more carefully, I realized that the simple java program on your home page (that I was following) uses a constructor for GatewayServer that assumes localhost as the server host:

GatewayServer server = new GatewayServer(app);

In order to run the server on another host, I also need to change my GatewayServer constructor call in my java code.  I should have sent you my java code before.  You would have realized my problem right away. Here is the new java code I used to start a server on a host other than localhost, using the default port, and not enabling any python callback functionality:

package demo;

import java.net.InetAddress;
import java.net.UnknownHostException;
import py4j.GatewayServer;

public class SimpleServer {

public int addition( int first, int second ) {
return first+second;
}

public static void main( String[] args) {
SimpleServer app = new SimpleServer();
GatewayServer server = null;
try {
InetAddress host = InetAddress.getLocalHost();
server = new GatewayServer(app, 25333, 0, host, null, 0, 0, null );
System.out.println( "GatewayServer for " + app.getClass().getName() + " started on " + host.toString() );
}
catch (UnknownHostException e) {
System.out.println( "exception occurred while constructing GatewayServer()." ); 
e.printStackTrace();
}
server.start();
}
}

It all works beautifully now.  Thank you so much!

Jim

Barthelemy Dagenais

unread,
Feb 10, 2016, 12:37:19 PM2/10/16
to Jim Hicks, Py4J Support and Comments
Hi Jim,

glad it worked for you. I definitively have to add an example showing how to run Py4J on specific IPs and ports.

Bart

Chaitanya Kumar

unread,
Jul 24, 2017, 11:55:33 AM7/24/17
to Py4J Support and Comments
Hi Bart,
Has an example been put up? I was struggling to get my python script connect to the Gateway Server remotely and stumbled across this.
If there exists an example for specific IP and ports, could you (or anyone else) please point me to it?

Thanks,
Chaitanya
Reply all
Reply to author
Forward
0 new messages