————————————————————————————————————————————————————————————
This tip shows you how to program a simple example of using the Java Native Interface. We will write a Java application that calls a C function to print "Hello World!".
The following steps are needed to develop this example:
- Creating a class (HelloWorld.java) that declares the native method.
- Using javac to compile the HelloWorld source file.
- Using javah -jni to generate a C header file (HelloWorld.h) containing the function prototype for the native method implementation.
- Writing the C implementation (HelloWorld.c) of the native method.
- Compiling the C implementation into a native library, creating Hello-World.dll or libHello-World.so.
- Running the HelloWorld program.
- Creating a class (HelloWorld.java) that declares the native method
- Using javac to compile the HelloWorld source file
- Using javah -jni to generate a C header file (HelloWorld.h) containing the function prototype for the native method implementation
- Writing the C implementation (HelloWorld.c) of the native method
- Compiling the C implementation into a native library, creating Hello-World.dll or libHello-World.so
- Running the HelloWorld program
We begin by writing the following program in the Java programming language. The program defines a class named HelloWorld that contains a native method, print.
class HelloWorld { |
The first part of the class definition is a static initializer that loads the native library containing the implementation of the print native method. This is followed by the declaration of the print native method and a main method that instantiates the Hello-World class and invokes the print native method for this instance.
After you have defined the HelloWorld class, save the source code in a file called HelloWorld.java. Then compile the source file using the javac compiler as follows:
javac HelloWorld.java
This command will generate a HelloWorld.class file in the current directory.
Next we will use the javah tool to generate a JNI-style header file that is useful when implementing the native method in C. You can run javah on the Hello-World class as follows:
javah -jni HelloWorld
The command shown above generates a file named HelloWorld.h.
The JNI-style header file generated by javah helps you to write C or C++ implementations for the native method. The function that you write must follow the -prototype specified in the generated header file. You can implement the Hello-World.print method in a C file HelloWorld.c as follows:
#include <jni.h> |
Now that all the necessary C code is written, you need to compile Hello-World.c and build this native library.
Different operating systems support different ways to build native libraries. On Solaris or Linux, the following command builds a shared library called libHello-World.so:
cc -G -I/java/include -I/java/include/solaris HelloWorld.c -o libHelloWorld.so
The -G option instructs the C compiler to generate a shared library instead of a regular Solaris executable file. Because of the limitation of page width in this book, we break the command line into two lines. You need to type the command in a single line, or place the command in a script file. On Win32, the following command builds a dynamic link library (DLL) HelloWorld.dll using the Microsoft Visual C++ compiler:
cl -Ic:\java\include -Ic:\java\include\win32 -MD -LD HelloWorld.c -FeHelloWorld.dll
The -MD option ensures that HelloWorld.dll is linked with the Win32 multithreaded C library. The -LD option instructs the C compiler to generate a DLL instead of a regular Win32 executable. Of course, on both Solaris/Linux and Win32 you need to put in the include paths that reflect the setup on your own machine.
Because the HelloWorld class contains its own main method, you can now run the program on Solaris or Win32 as follows:
java HelloWorld
You should see the following output:
Hello World!
It is important to set your native library path correctly for your program to run. The native library path is a list of directories that the Java virtual machine searches when loading native libraries. If you do not have a native library path set up correctly, then you see an error similar to the following:
java.lang.UnsatisfiedLinkError: no HelloWorld in library path at java.lang.Runtime.loadLibrary(Runtime.java) at java.lang.System.loadLibrary(System.java) at HelloWorld.main(HelloWorld.java)
Make sure that the native library resides in one of the directories in the native library path. If you are running on a Solaris system, the LD_LIBRARY_PATH environment variable is used to define the native library path. Make sure that it includes the name of the directory that contains the libHelloWorld.so file. If the libHelloWorld.so file is in the current directory, you can issue the following two commands in the standard shell (sh) or KornShell (ksh) to set up the LD_LIBRARY_PATH environment variable properly:
LD_LIBRARY_PATH=. export LD_LIBRARY_PATH
If you are running on a Windows 95 or Windows NT machine, make sure that HelloWorld.dll is in the current directory, or in a directory that is listed in the PATH environment variable.
In Java 2 SDK 1.2 release, you can also specify the native library path on the java command line as a system property as follows:
java -Djava.library.path=. HelloWorld
The "-D" command-line option sets a Java platform system property. Setting the java.library.path property to "." instructs the Java virtual machine to search for native libraries in the current directory.