Previous | Next | Trail Map | Integrating Native Code and Java Programs | Step By Step


Step 1: Write the Java Code

The following Java code segment defines a class named HelloWorld. This class has one method and a static code segment.
class HelloWorld {
    public native void displayHelloWorld();

    static {
        System.loadLibrary("hello");
    }
}

Define a Native Method

You must define all methods, whether Java methods or native methods, within a Java class. When you write a method in a programming language other than Java, you must include the keyword native as part of the method's definition within the Java class. The native keyword signals to the Java compiler that the function is a native language function. You can tell that the implementation for the HelloWorld class's displayHelloWorld() method is written in another programming language because of the native keyword that appears as part of its method definition:
public native void displayHelloWorld();
This method definition in your Java class provides only the method signature for displayHelloWorld(). It provides no implementation for the method. The implementation for displayHelloWorld() is provided in a separate native language source file.

The method definition for displayHelloWorld() also indicates that the method is a public instance method, accepts no arguments, and returns no value. For more information about arguments to and return values from native methods see Java Native Interface Programming(in the Integrating Native Code and Java Programs trail).

Load the Library

You compile the native language code that implements displayHelloWorld into a shared library (you will do this in Step 5: Create a Shared Library) and you also load it into the Java class that requires it. Loading the library into the Java class maps the implementation of the native method to its definition.

Use the System.loadLibrary method to load the shared library created when you compiled the implementation code. Place this method within a static initializer. The argument to System.loadLibrary is the shared library name. This can be any name that you choose. The system uses a standard, but platform-specific, approach to convert the library name to a native library name. For example, the Solaris system converts the library name "hello" to libhello.so, while a Win32 system converts the same name to hello.dll.

The following static initializer from the HelloWorld class loads the appropriate library, named hello. The runtime system executes a class's static initializer when it loads the class.

static {
    System.loadLibrary("hello");
}

Create the Main Program

You must also create a separate source file with a Java application that instantiates the class that contains the native method definition. This Java application will also call the native method. Because this is an application, it must have a main function. In a separate source file, which for this example we will name Main.java, create a Java application that instantiates HelloWorld and calls the displayHelloWorld() native method.
class Main {
    public static void main(String[] args) {
        new HelloWorld().displayHelloWorld();
    }
}
You can see from the code sample above that you call a native method in the same manner as you call a regular method: just append the name of the method to the end of the object name, separated with a period ('.'). A matched set of parentheses, ( and ), follow the method name and enclose any arguments to pass into the method. The displayHelloWorld() method doesn't take any arguments.


Previous | Next | Trail Map | Integrating Native Code and Java Programs | Step By Step