py4j without gateway in the java

42 views
Skip to first unread message

P S

unread,
Mar 17, 2023, 3:55:09 PM3/17/23
to Py4J Support and Comments
Hello is there a possibility to use py4j without having gateway started in a Java application?

Jonah Graham

unread,
Mar 17, 2023, 5:18:22 PM3/17/23
to py...@py4j.org
Short answer is no. However I suspect I don't fully understand your use case. If there is no gateway what do you want py4j to communicate with?

On Fri, Mar 17, 2023 at 3:55 PM P S <pavve...@gmail.com> wrote:
Hello is there a possibility to use py4j without having gateway started in a Java application?

--
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/6bdc33a8-e57e-40e4-9e53-380b527b4d3en%40py4j.org.
--
~~~
Jonah Graham (he/him)
Kichwa Coders
www.kichwacoders.com

P S

unread,
Mar 18, 2023, 1:19:00 AM3/18/23
to Py4J Support and Comments, Jonah Graham
Of course I would like to communicate with a Java application. But if possible I would like not to modify the Java application.

I saw a thread on a stackoverflow (py4j: how to launch the java Gateway from Python) where somebody wrote that in the below example: my_jar.jar doesn't have to start a gateway. Is that correct?
from py4j.java_gateway import JavaGateway
 gg = JavaGateway.launch_gateway(classpath="/path/my_jar.jar")
myclass_instance = gg.jvm.my_class_package_name.MyClass()
result = myclass_instance.my_method()

Jonah Graham

unread,
Mar 18, 2023, 2:45:50 PM3/18/23
to P S, Py4J Support and Comments
Yes, that launches the gateway for you - see the docs https://www.py4j.org/py4j_java_gateway.html#py4j.java_gateway.JavaGateway.launch_gateway - with the main being py4j.GatewayServer. So you will still need to modify the startup of your app at a minimum, potentially doing that with Python using the gateway to load and run your main class.

What happened when you tried the example on StackOverflow with your app? Did you get an error message, or other?


~~~
Jonah Graham (he/him)
Kichwa Coders
www.kichwacoders.com

Paweł Stefański

unread,
Mar 20, 2023, 2:14:49 AM3/20/23
to Jonah Graham, Py4J Support and Comments
Yes I saw the description but since I'm not very familiar with Java it is not obvious for me how example code for Java for the example from stack overflow should look like.
I'm getting an error:
image.png

And that is my small example which I'm trying to use.
image.png
I've commented gateway server line since it should be run by py4j.GatewayServer.
image.png
Please correct me if I'm doing something wrong in that example.


--
Paweł Stefański
AdditionApplication.java
main.py

Jonah Graham

unread,
Mar 22, 2023, 2:06:38 PM3/22/23
to Paweł Stefański, Py4J Support and Comments
Short answer is it works for me. Did you compile AdditionApplication into the py4j-java.jar? I recommend using a different name though. Try this based on your original example:

$ pip install py4j
Collecting py4j
  Downloading py4j-0.10.9.7-py2.py3-none-any.whl (200 kB)
     |████████████████████████████████| 200 kB 12.8 MB/s
Installing collected packages: py4j
Successfully installed py4j-0.10.9.7

$ cat main.py

from py4j.java_gateway import JavaGateway
gg = JavaGateway.launch_gateway(classpath="examples.jar")

myclass_instance = gg.jvm.examples.AdditionApplication()

print(myclass_instance.addition(2, 3))


$ cat examples/AdditionApplication.java
package examples;

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

$ javac examples/AdditionApplication.java
$ jar cf examples.jar examples/AdditionApplication.class
$ python main.py
5
$ python --version
Python 3.8.10

HTH,
Jonah



~~~
Jonah Graham (he/him)
Kichwa Coders
www.kichwacoders.com

Reply all
Reply to author
Forward
0 new messages