Previous | Next | Trail Map | Integrating Native Methods into Java Programs | Step By Step


Step 5: Write the C Function

Now, you can finally get down to the business of writing the implementation for the native method in C.

The function that you write must have the same function signature as the one you generated with javah into the HelloWorld.h file in Step 3: Create the .h File. The function signature generated for the HelloWorld class's displayHelloWorld() native method looks like this:

extern void HelloWorld_displayHelloWorld(struct HHelloWorld *);
Here's our implementation for HelloWorld_displayHelloWorld() which can be found in the file named HelloWorldImp.c.
#include <StubPreamble.h>
#include "HelloWorld.h"
#include <stdio.h>

void HelloWorld_displayHelloWorld(struct HHelloWorld *this) {
    printf("Hello World!\n");
    return;
}
As you can see, the implementation for HelloWorld_displayHelloWorld() is straightforward: the function uses the printf() function to display the string "Hello World!" and then returns.

This file includes 3 C header files:


Previous | Next | Trail Map | Integrating Native Methods into Java Programs | Step By Step