Integration with java

  • Thread starter Joachim Boomberschloss
  • Start date
J

Joachim Boomberschloss

Hello,

I am working on a project in Python, and I"m
currently
looking into the possibiliy of writing some of the
project"s modules in Java. Given that a large part of
the code is already written in Python, using the
standard libraries and several extension modules, I
am
trying to gauge the viability of integration with
Java
via Jython with minimal impact on present code, on
the
complexity of future code, and on deadlines!

I need to know in general how dirty it will be to
combine the two languages, and what the costs I
should
take into account are. I"m also interested to know if
there are any commonly accepted strategies to fully
switch project languages (I"m considering, in the
extreme case, to change it all into Java), and if
writing some modules in Java and replacing the rest
gradually makes sense.

Three more specific questions I thought of:
1. Is is possible to pack a Jython/Java program in a
py2exe-like fashion? Is it possible to pack in such a
way both Python, Jython and Java code?
2. Does transferring large data structures between
Java and Python code running by Jython have a
significant effect on memory? For example, would
passing the result of a large database query from
Java
to Jython, for further processing, cause the entire
data to be duplicated or something similar?
3. Did I miss anything fundemental?

Thanks!

Joe.


__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
 
I

Istvan Albert

Joachim said:
the code is already written in Python, using the
standard libraries and several extension modules

One thing to keep in mind is that Jython does not
integrate CPython, instead it "understands" python code
directly. So if you have a C extension that works with python
it won't work with Jython.

My feeling is that if you had a lot of Java code written and
wanted to build on that with python Jython would be a better
fit than vice versa.

Istvan.
 
C

Christopher De Vries

It is possible, though possibly painful, to call java modules from
CPython using JNI. This is more difficult than Jython integration, but
probably required if you want to keep using your extension modules.

The JNI tutorial is available at
http://java.sun.com/docs/books/tutorial/native1.1/index.html .

I probably would not take this approach unless java offered some
incredibly substantial benefit or I was integrating a complex python
system with a complex java sytem. I would also probably start by
creating a good C API to access the required java modules via JNI and
then use SWIG (http://www.swig.org/) to generate the python wrapper.

Of course if you can drop the extension modules you have already
written, accessing Java from Jython is just an import statement away.
Good luck,

Chris
 
C

Cameron Laird

One thing to keep in mind is that Jython does not
integrate CPython, instead it "understands" python code
directly. So if you have a C extension that works with python
it won't work with Jython.

My feeling is that if you had a lot of Java code written and
wanted to build on that with python Jython would be a better
fit than vice versa.

Istvan.

There are other possibilities, though, including JPE
<URL: http://jpe.sourceforge.net >. I recommend the
original poster consider the latter.
 
J

Jon Perez

Can someone summarize in a nutshell what is the
difference between JPype and JPE?
 
C

Cameron Laird

Can someone summarize in a nutshell what is the
difference between JPype and JPE?

JPE's the original. It provided more functionality than JPype has
achieved so far, I believe (though that could change any day). I
think no one now maintains JPE.

Someone really ought to include a couple of sentences to that effect
on the front page of <URL: http://jpype.sf.net/ >.
 
S

Steve Menard

Cameron said:
JPE's the original. It provided more functionality than JPype has
achieved so far, I believe (though that could change any day). I
think no one now maintains JPE.

Someone really ought to include a couple of sentences to that effect
on the front page of <URL: http://jpype.sf.net/ >.

Well, Cameron summed it up pretty good :)

I'd add that The only major (and yes I know it is VERY major)
funtionailty missing in JPype is the ability to subclass Java classes in
Python.

On the other hand JPype will (soon) have functionality that JPE doesnt
have. Java arrays can already (in 0.4) be iterated as regular Python
collections. Version 0.5 will add that same behavior for Java
collections (Map, List, Set, Iterator).

Of course, the above is based on the JPE documentation, because I havent
been able to get JPE to work.

About Cameron's suggestion, sure. I'll do it as soon as I (or someone
else) can get both JPype and JPE to work so they can be compared through
more than just their respective documentation.

Steve
a.k.a devilwolf on sourceforge
 
I

Istvan Albert

Cameron said:
Someone really ought to include a couple of sentences to that effect
on the front page of <URL: http://jpype.sf.net/ >.

Now I remember visiting this site, but never understood how it
actually worked. Examples such as:

from jpype import *
startJVM("d:/tools/j2sdk/jre/bin/client/jvm.dll", "-ea")
java.lang.System.out.println("hello world")
shutdownJVM()

in three different versions are the only code examples
that to show "complete" working snippets. I'm still
clueless as to how would one say share a list between
python and java.

Istvan.
 
J

Jan Dries

Istvan said:
Now I remember visiting this site, but never understood how it
actually worked. Examples such as:

from jpype import *
startJVM("d:/tools/j2sdk/jre/bin/client/jvm.dll", "-ea")
java.lang.System.out.println("hello world")
shutdownJVM()

in three different versions are the only code examples
that to show "complete" working snippets. I'm still
clueless as to how would one say share a list between
python and java.

A while ago there was a thread in c.l.p about using JPype to access JMS
from within Python. IIRC someone then contributed a more elaborate real
life example of how to do this. Search Google Groups for more details.

Regards,
Jan
 
S

Steve Menard

Istvan said:
Now I remember visiting this site, but never understood how it
actually worked. Examples such as:

from jpype import *
startJVM("d:/tools/j2sdk/jre/bin/client/jvm.dll", "-ea")
java.lang.System.out.println("hello world")
shutdownJVM()

in three different versions are the only code examples
that to show "complete" working snippets. I'm still
clueless as to how would one say share a list between
python and java.

Istvan.

I am sorry you find the site so confusing ... perhpas I shold post a
more complete example in a prominent location ...

To asnwer your question more fully, the jpype-specific cide is only for
looking up the Classes and startting/stopping the environment. For
everything else, Java objects and classes are used as regular Python
objects.

In version 0.4.x, the API mostly remain Java's. This means some of the
basic magic python methods have been mapped to java equivalent (like
__str__ mapped to toString() and __eq__ mapped to equals()).

If you have any questions, feel free to post them on the feedback list
on sourceforge. I check it every day and I try to answer as quickly as
possible.

Steve, aka devilwolf on sourceforge, maintainer of JPype
 
D

Dan Bishop

Istvan said:
One thing to keep in mind is that Jython does not
integrate CPython, instead it "understands" python code
directly. So if you have a C extension that works with python
it won't work with Jython.

Also, Jython is several versions behind CPython, so any Python code
that uses generators or new-style division won't work either.
My feeling is that if you had a lot of Java code written and
wanted to build on that with python Jython would be a better
fit than vice versa.

I agree.
 
S

Steve Holden

Dan said:
Also, Jython is several versions behind CPython, so any Python code
that uses generators or new-style division won't work either.




I agree.
Also let's not forget that the PSF has funded a project that's intended
to help Jython get up to the curve - see
http://www.python.org/psf/grants/Jython_PSF_Grant_Proposal.pdf. If that
project succeeds, and if 2.5 development *does* focus mostly on the
library, there's a sporting chance that Jython can be fully up to date
for the 2.5 release.

regards
Steve
 
I

Istvan Albert

Steve said:
To asnwer your question more fully, the jpype-specific cide is only for
looking up the Classes and startting/stopping the environment. For
everything else, Java objects and classes are used as regular Python
objects.

Thanks for the response. Currently I don't need to use java but
in the past when I explored such a possibility I looked at jpype
and I was unable to understand from the documentation what it
actually does.

There is a lot of text there, but it is all concerning catching
errors or other subtleties. For a new visitor the most important
question is about how it works, what does it do, and how can it be
applied for the given problem.
> everything else, Java objects and classes are used as regular Python
> objects.

This is too generic. My question was a little more specific,
how would I pass a python list as an argument of a java class/method or
transform a java list into a python one? You don't have to
answer it here, I'm just pointing out the kind of
questions that I was unable to get an answer for on the jpype
website.

best,

Istvan.
 
S

Steve Menard

Istvan said:
Thanks for the response. Currently I don't need to use java but
in the past when I explored such a possibility I looked at jpype
and I was unable to understand from the documentation what it
actually does.

There is a lot of text there, but it is all concerning catching
errors or other subtleties. For a new visitor the most important
question is about how it works, what does it do, and how can it be
applied for the given problem.


This is too generic. My question was a little more specific,
how would I pass a python list as an argument of a java class/method or
transform a java list into a python one? You don't have to
answer it here, I'm just pointing out the kind of
questions that I was unable to get an answer for on the jpype
website.

best,

Istvan.

I see what you mean. And I agree fully. I guess that's one more thing to
put on the TODO list hehe.

Thanks for the input.


Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
474,216
Messages
2,571,120
Members
47,720
Latest member
mohdkaif002

Latest Threads

Top