views:

755

answers:

7

I have a Java app that needs to integrate with a 3rd party library. The library is written in Python, and I don't have any say over that. I'm trying to figure out the best way to integrate with it. I'm trying out JEPP (Java Embedded Python) - has anyone used that before? My other thought is to use JNI to communicate with the C bindings for Python.

Any thoughts on the best way to do this would be appreciated. Thanks.

+4  A: 

Have you considered running Jython on the Java VM?

cletus
IronPython is for .NET, not Java. IKVM could unite the three potentially, but ick :)
Jon Skeet
Yes, got my wires crossed.
cletus
Have I missed a reference to IronPython elsewhere ?
Brian Agnew
Yes - in Cletus's answer before he edited it. (The edit doesn't show because it was within the first few minutes.)
Jon Skeet
Ah. Understood. Thx
Brian Agnew
+6  A: 

Why not use Jython? The only downside I can immediately think of is if your library uses CPython native extensions.

EDIT: If you can use Jython now but think you may have problems with a later version of the library, I suggest you try to isolate the library from your app (e.g. some sort of adapter interface). Go with the simplest thing that works for the moment, then consider JNI/CPython/etc if and when you ever need to. There's little to be gained by going the (painful) JNI route unless you really have to.

Jon Skeet
I've looked at the Jython libraries. The problem is that I have no guarantee that the libraries I'm using don't (or won't in the future) use native extensions.
Jeff Storey
I'll do that. Thanks. Do I need to add anything special to a py file to run it with Jython? Or if it's native python, should it just run?
Jeff Storey
I don't have much experience with Jython - consult the docs :)
Jon Skeet
Will do, appreciate the help.
Jeff Storey
+3  A: 

If you can get your Python code to work in Jython, then you should be able to use that to call it from Java:

ars
A: 

I've investigated a similar setup with JNI. Maybe this will help if haven't seen it yet:

http://wiki.cacr.caltech.edu/danse/index.php/Communication_between_Java_and_Python

http://jpe.sourceforge.net/

Ralphleon
+2  A: 

You could use a messaging service like ActiveMQ. It has both Python and Java support. This way, you can leave the complicated JNI or C bindings as they are and deal solely with what I consider a simple interface. Moreover, when the library gets updated, you don't need to change much, if anything.

geowa4
A: 
Marcin
A: 

My other thought is to use JNI to communicate with the C bindings for Python.

I like very much JNA:

JNA provides Java programs easy access to native shared libraries (DLLs on Windows) without writing anything but Java code—no JNI or native code is required. This functionality is comparable to Windows' Platform/Invoke and Python's ctypes. Access is dynamic at runtime without code generation.

My 0.02$ :)

dfa