Contents Prev Next

# 1 Accessing C Code from Java

The general procedure for accessing C code from Java is as follows:

- Define your class in Java using native methods to indicate that their implementation is found externally in your C routines.

- Compile the class source creating the .class file. See javac for more information on compiling .java files.

- Run javah on the class to generate a C structure that matches the Java class. (see javah for more information). javah creates a C header file that defines the data structures and function prototypes the C routines will need to implement the Java native methods.

- Run javah -stubs on the class to generate stub declarations (see javah for more information). The stubs are used by the Java interpreter to find the native method stub and marshal parameters between the Java interpreter and the C routines.

- Create a C file from the stubs file by putting the include file StubPreamble.h followed by the javah-generated stubs into a C file. Next, compile this file and put it into a dynamically loadable module.

- Define a C file that has the C routines that implement the native methods declared in your Java class. Compile this C file and add it to the dynamically loadable library that contains your compiled C stubs.

- The last step is linking your library into Java so that your dependent Java class can resolve it's native methods and execute. This final step is achieved by using the Java dynamic linker class. This class is in the java.util package. You must be sure to use Linker to load your library before you use any native methods from your Java class or you'll receive an UnsatisfiedLinkException exception.

Contents Prev Next

Implementing Native Methods

Generated with CERN WebMaker