This chapter uses a simple example to illustrate how to use JNI. We write a Java program and use it to call a C function to print "Hello World !".
2.1 Overview
Figure 2.1 demonstrates how to use a Java program to call the C function to print "Hello World !". This process includes the following steps:
1. Create a class (helloworld. Java) to declare the local method.
2. Use javac to compile the source file hollowworld. Java and generate helloworld. Class. Use javah-JNI to generate the C header file (helloworld. h), which contains the function prototype of the local method.
3. Use C code to write the implementation of the function prototype.
4, The C function is compiled into a local library, create a Hello-World.dll or libHello-World.so.
5. Run the helloworld program using Java commands. The class file helloworld. Class and the local library (helloworld. dll or libhelloworld. So) are loaded at runtime.
Figure 2.1 compile and run the "helloworld" program
The remaining parts of this chapter explain these steps in detail.