How to call Java from Python?

Dave Kuhlman dkuhlman at
Fri Jun 25 01:35:13 CEST 2004

Steve Menard wrote:


> Seems like my project my project got started at the right time :)
> Take a look at JPype
> it aims to do what JPE did (but work).
> It is still early in development, and working on win32 exclusively
> for now (the linux port should consist only of a few lines of
> code, but I do not have a linux meachine readily available). If
> you have a little C coding knowledge, you might even contribute it
> :)
> What's more, your's truly is doing his best to answer question and
> fix problems as they arise.
> I hope this helps,
> Steve Menard

JPype looks like the real solution to me.  And, I hope to use and
help with it.

However, I'm on Linux.  So, until JPype supports Linux, I've done
something a bit simpler (or maybe not) in the meantime, here is a

```` generates support files that enable
Python to use the classes and methods in a Java source code file.

This facility requires and uses ``gcj``, "The GNU Compiler for the
Java Programming Language".  It can be used only on platforms that
support ``gcj``, ``gcjh``, and ``libgcj``.

The basic strategy is the following:

1. Use ``gcjh`` to generate a header file for your Java class.

2. Write adapter classes, methods, and functions that use the CNI
   (the Compiled Native Interface) API to talk to your Java class.

3. Use SWIG to create Python wrappers for your adapter classes,
   methods, and functions.

4. Compile the resulting source code to produce Python extension
   (a shared library) that you can be import from Python.

```` helps you by generating some of the files
you need and by generating a ``Makefile`` that generates others.
*But*, you will need to write the C/C++ adapter classes, methods,
and functions by hand yourself.  And, this will require that you
know enough CNI to do so.

Where to get it -- You can find it at:

I'm interested in any suggestions, criticisms, ideas, etc.


Dave Kuhlman

More information about the Python-list mailing list