Writing a Java Program with Native Methods

For a complete JNI documentation and specification see SUN's JNI page: http://java.sun.com/docs/books/jni/html/jniTOC.html
This lesson walks you through the steps necessary to integrate native code with programs written in Java.

This lesson implements the canonical "JavaDriverJNI.java" program. The "JavaDriverJNI.java" program has one Java class, called JavaDriverJNI.java does two things:

  1. it declares TWO native methods that process String Arrays and Integer Arrays, and 
  2. it implements the main method for the overall package. The implementation for the native method is provided in C++.

Note :  This lesson assumes that you are starting with neither existing C++ functions nor Java classes. While "in the real world" you probably have existing C/C++/Fortran functions that you wish to integrate with Java programs, you will still need to modify the signatures for these external C++ functions to work with the JNI. To be sure that you use the correct signatures, it is best to begin by first writing and compiling the Java code, as described here.


Writing native methods for Java programs is a multi-step process.

  1. Begin by writing the Java program. Create a Java class that declares the native method; this class contains the declaration or signature for the native method. It also includes a main method which calls the native method.
  2. Compile the Java class that declares the native method and the main method.
  3. Generate a header file for the native method using javah with the native interface flag -jni. Once you've generated the header file you have the formal signature for your native method.
  4. Write the implementation of the native method in the programming language of your choice, such as C or C++.
  5. Compile the header and implementation files into a shared library file.
  6. Run the Java program.

The following figure illustrates these steps for the JavaDriverJNI.java program:

JNI Diagram

Step 1: Write the Java Code

Create a Java class named JavaDriverJNI that declares all native methods. This class also includes a main method that creates an object of type JavaDriverJNI and calls the native methods, as needed.

Step 2: Compile the Java Code

Use javac to compile the Java code that you wrote in Step 1.

Step 3: Create the .h File

Use javah to create a JNI-style header file (a .h file) from the JavaDriverJNI class. The header file provides a function signature for the implementation of the native methods processString & processArray (these are defines in two different C++ files, see below).

Step 4: Write the Native Method Implementation

Write the implementations for the native methods in native language (such as ANSI C, C++, Fortran) source files (CPPArrayFilterJNI.cpp & CPPArrayFilterJNI.cpp). The implementations will be regular functions (processArray & processString, respectively) that are integrated with your main Java class, JavaDriverJNI.

Step 5: Create a Shared Library

Use the C++ (or other) compiler to compile the header .h files and the source .c files that you created in Steps 3 and 4 into shared libraries. In Windows 95/NT terminology, a shared library is called a dynamically loadable library (DLL), in Unix these are .so files.

Step 6: Run the Program

And finally, use java JavaDriverJNI, the Java interpreter, to run the program.


    1. Script file (compile_run.bat) that compiles and test-runs this JNI protocol (on Windows, using the Borland's free C++ compiler, bcc, v. 5.5) is given below:
javac JavaDriverJNI.java
javah -jni JavaDriverJNI
bcc32 -I"C:\Program Files\JavaSoft\include" -IC:\Borland\BCC55\Include -LC:\Borland\BCC55\Lib -I"C:\Program Files\JavaSoft\include\win32" -WD CPPArrayFilterJNI.cpp -oCPPArrayFilterJNI.dll
bcc32 -I"C:\Program Files\JavaSoft\include" -IC:\Borland\BCC55\Include -LC:\Borland\BCC55\Lib -I"C:\Program Files\JavaSoft\include\win32" -WD CPPStringFilterJNI.cpp -oCPPStringFilterJNI.dll
java JavaDriverJNI arg1 arg2 ARG3
    2. You can also start the JVM and instantiate Java objects from within native methods (written in say C/C++). For detailed description and examples see this page: http://java.sun.com/docs/books/jni/html/invoke.html.

    3. Here is a ZIP file containing the sample code for this framework with a simple blurring filter.