Previous | Next | Trail Map | Integrating Native Methods into Java Programs | Table of Contents


Step By Step

This lesson walks you through the steps necessary to integrate native code with Java programs.

The example used throughout this lesson implements the canonical "Hello World!" program. The "Hello World!" program has two Java classes: the first implements the main() method for the overall program, and the second, called HelloWorld, has one method, a native method, that displays "Hello World!". The implementation for the native method is provided in the C programming language.

Step 1: Write the Java Code

Create a Java class, named HelloWorld, that declares a native method. Also, write the main program that creates a HelloWorld object and calls the native method.

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 .h file.

Step 4: Create a Stubs File

Now, use javah to create a stubs file.

Step 5: Write the C Function

Write the implementation for the native method in a C source file. The implementation will be a regular C function that's integrated with your Java class.

Step 6: Create a Dynamically Loadable Library

Use the C compiler to compile the .h file, the stubs file, and the .c file that you created in Steps 3, 4, and 5 into a dynamically loadable library.

Step 7: Run the Program

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


Previous | Next | Trail Map | Integrating Native Methods into Java Programs | Table of Contents