Erick Téllez
Erick Téllez

Reputation: 15

How can I send a data array back and forth between java and python?

I have a java program and I need it to get some data calculated by a python script.

I've already got java to send an integer to python via jython's PythonInterpreter and displayed it, but I can't recover it to make other operations. Also, it would be great to send a full integer array rather than a single integer but I can't wrap my mind arround PyObjects and how to use them.

Is there any useful tutorial that covers arrays? I've been searching for a while but I just find integer and float related tutorials.

Upvotes: 0

Views: 359

Answers (3)

higee
higee

Reputation: 377

I've worked on similar project. Here's brief outline of what Java and Python was doing respectively.

Java We used Java as a main server for receiving requests from clients and sending back responses after some data manipulation.

Python Python was in charge of data manipulation or calculation. Data was sent from Java via socket network. We first defined the data we needed in string format, then cncerted them into bytes in order to have them semt via socket network.

Since there were limitations, though, using socket network, I changed it to Rest Api using Python Flask. In that way we could easily communicate with, not only but in this case mainly, Java with key-value json format. In this way, I was able to recieve any data type that could be passed through Api including array object you mentioned.

Upvotes: 1

Irmen de Jong
Irmen de Jong

Reputation: 2847

If the solution of writing/reading the numbers to a file somehow is not sufficient, you can try the following: Instead of using Jython, you can use Pyro4 (and the Pyrolite client library for your java code) to call a running Python program from your java code. This allows you to run your python code in a 'normal' python 3.6 interpreter for instance, rather than being limited to what version Jython is stuck on. You'll have to launch the Python interpreter in a separate process though (but this could very well even be on a different machine)

Upvotes: 0

Matt
Matt

Reputation: 708

If you want a simple solution then I suggest that you write and read the integers to a file. Perhaps not the most elegant way but it would only take a couple of minutes to implement.

Upvotes: 1

Related Questions