Step 1: declare local variables
<CCID_NOBR>
<CCID_CODE> Example: class NativeHello {
Public native void nativeHelloWorld ();
Static {
System. loodlibrary ("nativeTest"); // call the nativeTest. dll library file
}
} </CCID_CODE> </CCID_NOBR>
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)
<CCID_NOBR>
<CCID_CODE> # include <stdio. h>
# Include <NativeHello. h> // indicates the. h file generated in step 2.
# Include <stubpreamble. h> // indicates the file under JDK include.
Void Nativehello_nativeHelloWorld (struct HNativeHello * this ){
.........
}/</CCID_CODE> </CCID_NOBR>
* 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:
<CCID_NOBR>
<CCID_CODE> class UseNative {
Public static void main (String [] args ){
NativeHello nh = new NativeHello ();
Nh. nativeHelloWorld ();
}
} </CCID_CODE> </CCID_NOBR>
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service