How to call java from python?

Mats Wichmann xyzmats at laplaza.org
Wed Aug 8 14:34:33 EDT 2001


On Wed, 08 Aug 2001 12:05:05 +0100, Ype Kingma
<ykingma at accessforall.nl> wrote:

:
:
:Larry Han wrote:
:> 
:> Is there any way to use java method from python?
:> Like calling C from python, it seems reasonable to
:> drag java and python closer.
:
:Absolutely: www.jython.org
:In jython you can do eg.:
:
:  import java
:
:  jht = java.util.Hashtable()
:
:  jht.put(1,'Hello world')
:
:  print jht.get(1)

Or... you can treat your Hashtable instance like a Python dictionary:

>>> jht = java.util.Hashtable()
>>> jht[1] = 'Hello world'
>>> print jht[1]
'Hello world'
>>> print jht
 {1=Hello world}


Neat stuff.  You can also call back into Jython from Java by mkaing a
PythonInterpreter instance.

Jython makes fooling with Java stuff much easier, for me anyway.  Just
this morning a new issue of the JDC (Java Developer Connection)
newsletter landed in my inbox, and spent time talking about
"performing exact calculations with floating point numbers" (a topic
which gets bandied about in this group now and then after someone does
something like 56 * 0.1 and gets 5.600000000005).  The article talks
about using BigDecimal.  I decided to fool with the examples they
described, but did in interactively in Jython...  cool.

Mats




More information about the Python-list mailing list