61

Possible Duplicate:
Java Python Integration

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

I'm aware of the Jython project, but it looks like this represents a way to use Java and its libraries from within Python, rather than the other way round - am I wrong about this?

If not, what would be the best method to interface between Java and Python, such that (ideally) I can call a method in Python and have the result returned to Java?

2

7 Answers 7

34

I'm aware of the Jython project, but it looks like this represents a way to use Java and its libraries from within Python, rather than the other way round - am I wrong about this?

Yes, you are wrong. You can either call a command line interpreter to run python code using Jyton or use python code from Java. In the past there was also a python-to-Java compiler, but it got discontinued with Jython 2.2

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks - I like the interpreter approach with interface the best, but I'll have to make a call on whether I can afford the performance hit vs. a compiled approach. But thank you and all answerers.
Just to add that the jythonc approach has been deprecated in favor of calling the python interpreter directly
Just to straight things up, you would still need the python environment/libraries/etc installed in your system?
For those looking at this answer, bear in mind it's from '09 and jythonc "doesn't support Python 2.3 and later features such as generators".
Is this answer still valid in 2017, i.e using the interpreter is prefered?
|
6

I would write a Python module to handle the text and language processing, and then build a small bridge in jython that your java program can interact with. The jython bridge will be a very simple one, that's really only responsible for forwarding calls to the python module, and return the answer from the python module to the java module. Jython is really easy to use, and setup shouldn't take you more than 15 minutes.

Best of luck!

2 Comments

i like this idea. can u point to an example pls
or else why do u need jython to call a python script when you can use Process.exec or ProcessBuilder
4

I don't think you could use NLTK from Jython, since it depends on Numpy which isn't ported to the JVM. If you need NLTK or any other native CPython extension, you might consider using some IPC mechanism to communicate between CPython and the JVM. That being said, there is a project to allow calling CPython from Java, called Jepp:

http://jepp.sourceforge.net/

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

sourceforge.net/projects/jpype/

pypi.python.org/pypi/javaclass/0.1

I've never used any of these project, so I cant't vow for their quality.

Comments

2

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

I don't know the environment that you're working in, but be aware that mixing languages in the same app can quickly lead to a mess. I recommend creating Java interfaces to represent the operations that you plan to use, along with separately-packaged implementation classes that wrap the Python code.

Comments

2

IN my opinion, Jython is exactly what you are looking at.
It is an implementation of Python within the JVM; as such, you can freely exchange objects and, for instance, inherit from a Java class (with some limitations).

Note that, its major strength point (being on top of of JVM) is also its major drawback, because it cannot use all (C)Python extension written in C (or in any other compiled language); this may have an impact on what you are willing to do with your text processing.

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

Comments

1

Simply run the Python interpreter as a subprocess from within Java.

Write your Python functionality as a proper script, which reads from stdin and writes to stdout.

Use the Java Runtime class to spawn a subprocess that runs your Python script. This 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 overhead.

4 Comments

This isn't a really nice way to interface between the 2, they will not be able to call a method in Pyton and have it output in Java.
I'd say the interface is the opposite of clean, since the communication is completely unstructured and requires additional parsing logic.
Agree:It can get a bit messy using Java Runtime: avoid it unless you really have to. You have to be careful not to introduce portability problems , you have to handle the return status; you may have to handle misbehaving processes etc; you lose a lot of control doing this.
I think this is the best solution for most of the cases. You have to deal with the output format anyways.
0

Remember to first check from those paying for the development that they're OK with the codebase needing a developer who knows both Python and Java from now on, and other cost and maintainability effects you've undoubtedly already accounted for.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.