Next

# Implementing Native Methods

This document explains how to implement native methods in Java applications. The demo application shown here does some simple file I/O. The sources for the demo application are available in:

    hotjava_install_dir/doc/native/

When writing native methods certain Java header files must be included. These header files are in:

    hotjava_install_dir/include

Please see the Makefile, included as part of the demo application, for a sample of which header files you need to include to implement native methods.

In addition to the files shown in this document the demo application also includes:

- Replace.class (and .java). Replace.class is the main program that you pass to java, which is the interpreter.

- A Makefile

- The dynamic library, called libfile.so

- libstubs*, which is a file that contains the javah-generated stubs and includes some important Java source header files.

The native modifier applies to methods and indicates that a method is implemented in C. (See The Java Language Specification for information on native methods.)

The reason you may need native methods is that Java applications sometimes require system resources and functions that are unavailable from the Java language and base classes. To accomodate this, Java provides for runtime native library loading for libraries written in C. To include a native library written in C into Java you create an Java class wrapper for the C code and dynamically link the library into the Java interpreter via the Linker class.

## Table of Contents

1 - Accessing C Code from Java
2 - Beginning the Sample Application
3 - Defining The Java Classes
4 - Generating Header Files
5 - Generating C Stubs
6 - Defining the C Interface
7 - Building the Library
8 - Linking the C Library into Java

Next

Generated with CERN WebMaker