Step 1: declare local variables
Example: Class nativehello {public native void nativehelloworld (); static {system. loodlibrary ("nativetest"); // call the nativetest. dll library file }}
|
Step 2: generate the header file
Use javac to compile nativehello. Java and javah to generate the C header file. h file.
Step 3: generate the root file
The command is as follows: javah-stubs nativehello (generate the nativehello. c file)
Step 4: Compile the C program (assuming the file name is nativetest. c)
# Include <stdio. h> # include <nativehello. h> // It is generated in step 2. hfile # include <stubpreamble. h> // The Void nativehello_nativehelloworld (struct hnativehello * This) file under JDK include ){.........}/
|
* The function name nativehello_nativehelloworld cannot be specified arbitrarily. You can find it from the header file generated by javah. You can also use the following method to name it: Class name_local method name (struct H class name * This )*/
Step 5: Compile the DLL file
Compile nativetest. C and nativehello. c into a dll library file with the same name as the file in system. loodlibrary ("nativetest ").
Finally, let's talk about the test method. The source file is as follows:
class UseNative{ public static void main(String []args){ NativeHello nh=new NativeHello(); nh.nativeHelloWorld(); } }
|
Http://java.sun.com/j2se/1.3/docs/tooldocs/solaris/javah.html