Accessing Java Class saved in Jar file from Python code

1,208 views
Skip to first unread message

Juliette

unread,
Jun 13, 2017, 6:07:49 PM6/13/17
to Py4J Support and Comments
Hi,

I have an already java object that I would like to use in my python code.The java object and its methods are in Jar file. 
I am trying to use Py4J to accomplish this, but I am having trouble with setting up the gateways between the JMV and python.
Since I only need to use the java object and its methods,is it necessary to have JMV running the java class in order to be accessible to Py4J? Is it possible to just import the class from java into python using Py4J? 

Thanks and looking forward to your reply.

Juliette 

Barthelemy Dagenais

unread,
Jun 13, 2017, 9:10:34 PM6/13/17
to Juliette, Py4J Support and Comments
Hi Juliette, Py4J only transmits messages between Python and Java.
Each side is responsible for executing its respective code. Your java
classes need to be executed in a JVM so you need to start the JVM to
create an instance and call its methods. Py4J includes a few methods
(e.g., JavaGateway.launch_gateway) to help you start a JVM. Otherwise,
there are other Python-to-Java bridges that will start the JVM for you
(I think JPype does that).

HTH,
Barthelemy
> --
> 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/09715a75-bb2e-40c2-a84e-4c501ac13de0%40py4j.org.

Federico Picetti

unread,
May 4, 2018, 5:59:43 AM5/4/18
to Py4J Support and Comments
Hi, I'm having the same problem. I could start a new subprocess or use an external bridge, but I don't want to have too many dependencies.
I tried to use launch_gateway, but how to pass the executable jar or the libraries?
Should I use the classpath parameter? I don't understand how to set it. Any suggestion?
Thank you!
Reply all
Reply to author
Forward
0 new messages