The JavaTM Tutorial

| Start of Tutorial > Start of Trail > Start of Lesson | Search Feedback Form

Trail: Java Native Interface
Lesson: Writing a Java Program with Native Methods

## Step 3: Create the .h File

> In this step, you use the `javah` utility program to generate a header file (a `.h` file) from the `HelloWorld` class. The header file provides a C function signature for the implementation of the native method `displayHelloWorld` defined in that class.

> Run `javah` now on the `HelloWorld` class that you created in the previous steps.

> The name of the header file is the Java class name with a `.h` appended to the end. For example, the command shown above will generate a file named `HelloWorld.h`.

> By default, `javah` places the new `.h` file in the same directory as the `.class` file. Use the `-d` option to instruct `javah` to place the header files in a different directory.

### The Function Definition

Look at the header file `HelloWorld.h`.

>     /* DO NOT EDIT THIS FILE - it is machine generated */
>     #include <jni.h>
>     /* Header for class HelloWorld */

>     #ifndef _Included_HelloWorld
>     #define _Included_HelloWorld
>     #ifdef __cplusplus
>     extern "C" {
>     #endif
>     /*
>      * Class:     HelloWorld
>      * Method:    displayHelloWorld
>      * Signature: ()V
>      */
>     JNIEXPORT void JNICALL Java_HelloWorld_displayHelloWorld
>       (JNIEnv *, jobject);

>     #ifdef __cplusplus
>     }
>     #endif
>     #endif

The `Java_HelloWorld_displayHelloWorld` function provides the implementation for the `HelloWorld` class's native method `displayHelloWorld`, which you will write in Step 4: Write the Native Method Implementation. You use the same function signature when you write the implementation for the native method.

If `HelloWorld` contained any other native methods, their function signatures would appear here as well.

The name of the native language function that implements the native method consists of the prefix `Java_`, the package name, the class name, and the name of the native method. Between each name component is an underscore "_" separator. Graphically, this looks as follows:

The native method `displayHelloWorld` within the `HelloWorld` class becomes `Java_HelloWorld_displayHelloWorld`. No package name appears in our example because `HelloWorld` is in the default package, which has no name.

Notice that the implementation of the native function, as it appears in the header file, accepts two parameters even though, in its definition on the Java side, it accepts no parameters. The JNI requires every native method to have these two parameters.

The first parameter for every native method is a `JNIEnv` interface pointer. It is through this pointer that your native code accesses parameters and objects passed to it from the Java application. The second parameter is `jobject`, which references the current object itself. In a sense, you can think of the `jobject` parameter as the "this" variable in Java. For a native instance method, such as the `displayHelloWorld` method in our example, the `jobject` argument is a reference to the current instance of the object. For native class methods, this argument would be a reference to the method's Java class. Our example ignores both parameters. The next lesson, Getting Started , describes how to access data using the JNI interface pointer `env` parameter. The next lesson also provides more information about `jobject`.

| Start of Tutorial > Start of Trail > Start of Lesson | Search Feedback Form

Copyright 1995-2004 Sun Microsystems, Inc. All rights reserved.