Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

passing data between java and python

Apologies if my question is noob. I'm running Django 1.2 with pgsql 8.4 and I'm required to run a Java program after getting the inputs from the user, does some calculations and return the results back to the user.

May I know what is the best way to pass data between the Java and Python Django, such that the above scenario are taken care of? I heard of Jython, but according to the Django docs, it requires the extension jython-Django, but currently it doesn't support django 1.2.x. Should I pass the data using stdout?

thanks people for hearing me out.

like image 354
goh Avatar asked Nov 01 '10 10:11

goh


People also ask

How do you communicate between Java and Python?

So here we use JSON to communicate between two programs called Python and Java.

Can Python and Java work together?

The seamless interaction between Python and Java allows developers to freely mix the two languages both during development and in shipping products.

How do I get data from Python to Java?

There are two options for communicating information from a Python process to a Java process: Java's popular networking libraries will make it easy to read in data through a socket. Since you can easily open up a socket in Python, implementing socket communication would be the easiest solution.

Can you run a Python script through Java?

javabridge. CPython, that can be used to execute Python code. The class can be used within Java code called from the Python interpreter or it can be used within Java to run Python embedded in Java. The CPython class binds the Python interpreter to the JVM and provides the ability to execute Python scripts.


1 Answers

If all you are doing is calling Java for some calculations, then subprocess is the easiest way, but I just wanted to throw another option out there.

JPype is a package that lets you run a JVM inside your Python program while calling the Java code as if it were Python (example).

That is, it's like Jython in that you call Java code directly and like subprocess in that you are actually running CPython. Consider JPype if for some reason the Java code is not easy to call from a shell script (aka, subprocess) or if a lot of two-way communication is necessary.

like image 133
ianmclaury Avatar answered Sep 26 '22 00:09

ianmclaury