SWIG Java Examples

$Header: /cvs/projects/SWIG/Examples/java/Attic/index.html,v 2002/07/28 20:31:31 cheetah Exp $

The following examples illustrate the use of SWIG with Java.

  • simple. A minimal example showing how SWIG can be used to wrap a C function, a global variable, and a constant.
  • native. Comparing the manual and the SWIG approach to calling native code.
  • typemap. Modifying the Java module's default behaviour by using typemaps.
  • constants. This shows how preprocessor macros and certain C declarations are turned into constants.
  • variables. An example showing how to access C global variables.
  • enum. Wrapping enumerations.
  • class. How to wrap a simple C++ class.
  • reference. C++ references.
  • pointer. Simple pointer handling.
  • template. C++ templates.
  • funcptr. Pointers to functions.

Running the examples

Please see the Windows page in the main manual for information on using the examples on Windows.

On Unix most of the examples work by making the Makefile before executing the program The Makefile will output the swig generated JNI c code as well as the Java wrapper classes. Additionally the JNI c/c++ code is compiled into the shared object (dynamic link library) which is needed for dynamic linking to the native code. The Makefiles also compile the Java files using javac.

Ensure that the dynamic link library file is in the appropriate path before executing the Java program. For example in Unix, must be in the LD_LIBRARY_PATH.

A Unix example:

$ make
$ export LD_LIBRARY_PATH=. #ksh 
$ java main


The examples have been extensively tested on the following platforms:
  • Solaris
The examples have been extensively tested using Sun's JDK on:
  • Sparc Solaris 2.6.
Your mileage may vary. If you experience a problem, please let us know by sending a message to