Using python libraries in a Java Program

Steve Menard steve.menard at videotron.ca
Sat Jul 3 20:10:56 EDT 2004


Sam Roth wrote:
> gohaku wrote:
> 
>> Hi everyone,
>> I would like to know if it's possible to use python (libraries) in a
>> Java Program.
>> I do not want to use: Process proc = Runtime.getRuntime.exec(command)
>>
>> I am basically looking for something like the "Reversed" version of 
>> Jython.
>>
>> Thanks in advance.
>> -gohaku
>>
>>
> I think you can use Jython that way. Just compile your module to a 
> .class file using jythonc, then use it like any other class. If you want 
> to access python modules put "import org.python.modules.MODULENAME.*" in 
> your Java program.

For this to work though, the Python class must implement or extend a 
Java interface/class. Otherwise a pure python class compiled to .class 
has a very strange interface indeed.

Also, if the needed python module is a native one, or relies on native 
code. you're out of luck.

Depending on the type of integration needed, and the type of module to 
integrate. I see that you have 2 choices. Either build a simple JNI 
module to embed the Python interprter in Java. Expose a few methods like 
execfile and you have simple integration that allows you to call any 
python module.

Tighter integration would be very difficult indeed. I am currently doing 
the same thing, except in reverse (hosting the JVM inside python) and 
its a pretty complicated task. I guess you could use my project to host 
your whole program in Python, then make callbacks back into ptyhon code 
to use the module. Then again, you could do the same using jython ...

Good luck,

Steve



More information about the Python-list mailing list