|
|
Table of Contents |
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, called
Main, implements themain()method for the overall program. The second, calledHelloWorld, 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 namedHelloWorldthat declares a native method. Also, write the main program that creates aHelloWorldobject and calls the native method.Step 2: Compile the Java Code
Usejavacto compile the Java code that you wrote in Step 1.Step 3: Create the .h File
Usejavahto create a header file (a.hfile) from theHelloWorldJava class. The header file defines a structure that represents theHelloWorldclass on the native language side, and provides a function definition for the implementation of the native methoddisplayHelloWorld()defined in that class.Step 4: Create a Stubs File
Now, usejavahto create a stubs file. The stubs file contains native language code that provides the glue that holds the Java class and its parallel native language structure together.Step 5: Write the Native Method Implementation
Write the implementation for the native method in a native language source file. The implementation will be a regular function that's integrated with your Java class.Step 6: Create a Shared Library
Use the compiler to compile the.hfile, the stubs file, and the.cfile that you created in Steps 3, 4, and 5 into a shared library. In Windows 95/NT terminology, a shared library is called a dynamically loadable library (DLL).Step 7: Run the Program
And finally, usejava, the Java interpreter, to run the program.
|
|
Table of Contents |