# SWIG Java Examples

`/cvsroot/SWIG/Examples/java/index.html,v 1.4 2004/01/20 21:27:27 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.
- callback. C++ callbacks using directors.
- extend. Polymorphism using directors.

## 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 main.java. 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, libexample.so must be in the LD_LIBRARY_PATH.

A Unix example:

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

## Compatibility

The examples have been extensively tested on the following platforms:

- Solaris
- Linux
- Cygwin
- Windows

Your mileage may vary. If you experience a problem, please let us know by sending a message to swig-dev@cs.uchicago.edu.