Python/Java Wrapper Generator

Author: Dave Kuhlman
Address:
dkuhlman@rexx.com
http://www.rexx.com/~dkuhlman
Revision: 1.0a
Date: June 24, 2004
Copyright: Copyright (c) 2004 Dave Kuhlman. This documentation is covered by The MIT License: http://www.opensource.org/licenses/mit-license.

Abstract

This document describes generate_wrappers.py: a python script that generates files for Python wrappers for Java a class. This facility uses and requires gcj, libgcj, and CNI.

Contents

1���Introducing generate_wrappers.py

generate_wrappers.py 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.

generate_wrappers.py 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.

2���Requirements and Limitations

The development strategy supported by generate_wrappers.py only works on the platforms supported by gcj and libgcj. See the following for a list of supported platforms: http://gcc.gnu.org/java/status.html.

In order to do development with generate_wrappers.py, you must have the following installed:

Limitation -- I'm not too sure about this, but I believe that this solution limits you to the sub-set of the JDK class libraries that are supported by gcj and libgcj. For more on this, see: http://gcc.gnu.org/java/faq.html#2_1.

3���Where to Get generate_wrappers.py

You can find generate_wrappers.py here: http://www.rexx.com/~dkuhlman/generate_wrappers-1.0a.tar.gz.

4���Examples

There is a simple example of the use of generate_wrappers.py in the distribution under the Examples directory. See the README file included with the example.

5���How to Use generate_wrappers.py

5.1���Step 1

Run generate_wrappers.py from the command line. Give it the base name of your Java class file. For example, if your Java class is in a file named "myclass.java", then you might run the following:

$ python generate_wrappers.py myclass

Here is the usage information for generate_wrappers.py:

/w1/Java/Wrapper_generator [34] ./generate_wrappers.py --help
usage:
    python generate_wrappers.py [options] <base_file_name>
example:
    python generate_wrappers.py my_java_class_file

options:
  -h, --help         show this help message and exit
  -v, --verbose      produce verbose output
  -f, --force        force overwrite of files.  Do not ask.
  -p, --python-impl  generate empty implementation files
p, python-impl
Generate mostly empty files for the adapter code. You will add code to these files that are wrappers or adapter classes, methods, and functions. The functions and methods in these files will be called by Python (through code generated by SWIG) and will in turn call the Java code (through CNI). Since you will add your own code to these files, I've required that you use an explicit switch to generate them, hopefully making it less likely that you will over-write your code.

5.2���Step 2

Add (and later modify as necessary) your adapter code. When you run generate_wrappers.py, it prints out a message telling you where (in which files) to add your adapter code.

5.3���Step 3

Run make to generate and compile additional code.

5.4���Step 4

Run Python to test your code.

5.5���Step 5

Repeat steps 2 through 4 as necessary.

6���See Also

Python: The Python home page.

gcj: The GNU Compiler for the Javatm Programming Language

libgcj: The libgcj home page

Java Technology: The Sun Java support site

SWIG: More information about SWIG, the software wrapper interface generator.