Python Forum
[Share] Invented a new wheel for calling Python from JAVA
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Share] Invented a new wheel for calling Python from JAVA
#1
I'm a java developer and need call Python from JAVA sometimes. I google that but didn't find a satisfy way to do it. The Jython is clear and simple, but it is not CPython and doesn't support external C package, also slower than CPython. Process and Runtime method is not easily to get a well format result. Other bridge solutions solve such problems, but inject additional codes. So finally I decided to invent the wheel myself, took me 5 nights, now it works fine in my project, so I hope it has a little help for you too. It is open source and can find it on GitHub: jpserve (https://github.com/johnhuang-cn/jpserve)

JPserve provides a simple way to call Python and exchange the result by well format JSON, few performance loss.

The following is a simple sample.
At first, install jpserve by "pip -install jpserve" and start jpserve on Python side, jpserve is a script executor server executes the script from JAVA side.

from jpserve.jpserve import JPServe
serve = JPServe(("localhost", 8888))
serve.start()
Output:
INFO:JPServe:JPServe starting... INFO:JPServe:JPServe listening in localhost 8888
Then call Python from JAVA side:
PyServeContext.init("localhost", 8888);
PyExecutor executor = PyServeContext.getExecutor();
script = "a = 2\n"
         + "b = 3\n"
         + "_result_ = a * b";
    
PyResult rs = executor.exec(script);
System.out.println("Result: " + rs.getResult());
Output:
Result: 6
Reply
#2
Did you try Swig?
Reply
#3
What about reading and writing to FIFO file?
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#4
(Oct-13-2016, 04:05 PM)wavic Wrote: What about reading and writing to FIFO file?
That doesn't sound like a very clean solution to me. Lots of weird issues can come up. Local web server is definitely a reasonable solution, especially since Python is dynamically typed anyway, but Swig (if it would work) would probably have less overhead and be more robust, since there isn't a web server to fall over.
Reply
#5
As I know Swig mainly for c/c++ call other languages. It is too complicated and high threshold.  It needs generate the wrapper interface before you can using it, too many additional work to do.

For jpserver, we can enhance the local server to make it more robust, auto recovery when fail. Till now, I haven't found it failed in my project.

I think the biggest benefit is clean and simple. The Python code can be developed by Python developer and deploy to Python environment, then JAVA developer just call it by name to get the result. Another benefit is the Python server can run in different server, JAVA can call it just like RMI.

I didn't test FIFO, but it should do in theory. JAVA just send the script to a standalone Python and execute it, then return the result to JAVA through socket connection. So it can execute anything that original Python can do.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Portability in Python and Java ankitdixit 1 1,156 Jun-30-2022, 12:04 PM
Last Post: snippsat
  Can the video calling feature be used from python telegram bot api? Kumarkv 0 1,849 May-30-2020, 01:34 PM
Last Post: Kumarkv
  Python Finally Overtakes Java on Github Skaperen 0 1,522 Nov-09-2019, 09:36 PM
Last Post: Skaperen
  Whether or not it needs Java for Android with Python Aashuraa 4 3,149 Nov-26-2018, 02:36 PM
Last Post: Aashuraa
  GUI, Games and Java haazimoto 2 2,878 Aug-22-2018, 06:16 PM
Last Post: haazimoto
  java book bundle metulburr 0 2,782 Nov-21-2017, 12:34 AM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020