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


Messages In This Thread
[Share] Invented a new wheel for calling Python from JAVA - by john.huang - Oct-13-2016, 09:01 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Portability in Python and Java ankitdixit 1 1,215 Jun-30-2022, 12:04 PM
Last Post: snippsat
  Can the video calling feature be used from python telegram bot api? Kumarkv 0 1,943 May-30-2020, 01:34 PM
Last Post: Kumarkv
  Python Finally Overtakes Java on Github Skaperen 0 1,590 Nov-09-2019, 09:36 PM
Last Post: Skaperen
  Whether or not it needs Java for Android with Python Aashuraa 4 3,278 Nov-26-2018, 02:36 PM
Last Post: Aashuraa
  GUI, Games and Java haazimoto 2 2,946 Aug-22-2018, 06:16 PM
Last Post: haazimoto
  java book bundle metulburr 0 2,834 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