Using Python from Java

Possible duplicate:
Python Java Integration

I have a large existing code base written in 100% Java, but I would like to use Python for some new sections. I need to do some text and language processing, and I would rather use Python and a library like NLTK to do this.

I know the Jython project, but it seems like this is a way to use Java and its libraries from Python, and not vice versa. Am I wrong about this?

If not, what would be the best way to interact between Java and Python, so that (ideally) can I call a method in Python and return the result in Java?

Thank.

+49
java python jython rpc
Jul 22 '09 at 12:04
source share
7 answers

I know the Jython project, but it looks like this is a way to use Java and its libraries from within Python, and not vice versa round - am I wrong?

Yes, you are mistaken. You can either invoke the command line interpreter to run python code using Jyton or use python code from Java . There was also a python-to-Java compiler in the past, but it was discontinued using Jython 2.2

+33
Jul 22 '09 at 12:14
source share

I would write a Python module for text and language processing, and then build a small bridge in jython that your Java program can interact with. The jython bridge will be very simple, which is really only responsible for forwarding calls to the python module and returns a response from the python module to the java module. Jython is very easy to use and the setup should not take you more than 15 minutes.

Good luck

+6
Jul 22 '09 at 12:12
source share

I don’t think you could use NLTK from Jython, as it depends on Numpy, which does not migrate to the JVM. If you need NLTK or any other native CPython extension, you might consider using some IPC mechanism for communication between CPython and the JVM. However, there is a project that allows you to call CPython with Java, called Jepp:

http://jepp.sourceforge.net/

The callback (calling Java code from CPython) is the goal of JPype and javaclass:

sourceforge.net/projects/jpype/

pypi.python.org/pypi/javaclass/0.1

I have never used any of these projects, so I cannot swear on their quality.

+4
Jul 22 '09 at 15:21
source share

Jython is a Python implementation running on the JVM. You can find information on embedding Python in an existing Java application in the user guide .

I do not know the environment in which you work, but remember that mixing languages ​​in one application can quickly lead to confusion. I recommend creating Java interfaces to represent the operations you plan to use, as well as separately packaged implementation classes that wrap Python code.

+2
Jul 22 '09 at 12:11
source share

In my opinion, Jython is exactly what you are looking at.
This is a Python implementation in the JVM; Thus, you can freely exchange objects and, for example, inherit from the Java class (with some restrictions).

Note that its main strength point (located on top of the JVM) is also its main drawback, as it cannot use the entire (C) Python extension written in C (or any other compiled language); this may affect what you are prepared to do with word processing.

For more information on what Jython is, its potential and its limitations, I suggest you read the Jython FAQ .

+2
Jul 22 '09 at 12:29
source share

Just run the Python interpreter as a subprocess from Java.

Write your Python functionality as a valid script that reads from stdin and writes to stdout.

Use the Java Runtime class to create a subprocess that runs your Python script. It is very simple to do and provides a very clean interface.




Edit

 import simplejson import sys for request in sys.stdin.readlines(): args = simplejson.loads( request ) result = myFunction( args['this'], args['that'] ) sys.stdout.writeline( simplejson.dumps( result ) + "\n" ) 

The interface is simple, structured and very low.

0
Jul 22 '09 at 12:10
source share

Do not forget to first check with those who pay for development that they are in order with a code base that requires a developer who knows both Python and Java from now on, and other cost and maintainability effects that you have undoubtedly taken into account.

See: http://www.acm.org/about/se-code 1.06, 2.03, 2.09, 4.03, 4.05, 6.07

0
Jul 22 '09 at 17:45
source share



All Articles