An example of the Java Native method is studied. I read the documents online.
First, let's take a look at what native method is. Refer to http: // www.80 × 86. CN/Article. asp? Article on ID = 1448
Simply put, a native method is the Java interface to non-Java code. it is Java's link to the "outside world. "More specifically, a native method is a Java method whose implementation is provided by non-Java code, most likely C. This means that the native method calls other non-Java code through Java.
In your Java class, you mark the methods you wish to implement outside of Java with the native method modifier-much like you wocould use the public or static modifiers. then, rather than supplying the method's body, you simply place a semicolon in its place. the only thing special about this Declaration is that the keyword native is used as a modifier. every other Java method modifier can be used eon G with native, comment t abstract. It refers to the native keyword, which is the same as other public static modifiers. Except abstract. This is logical, because the native modifier implies that an implementation exists, and the abstract modifier insists that there is no implementation. your native methods can be static methods, thus not requiring the creation of an object (or instance of a class ). this is reasonable, because native implies that these methods have implementation bodies, but these implementations are non-Java, but abstract clearly specifies that these methods have no implementation bodies. Haha. I will not translate the original text. If you are interested, go to the original article.
After checking some information, I wrote the following code. This code is helloworld again. Alas, continue with helloworld. Use Java to call a method implemented in C.
Code:
- /**
- * Project_name: nativetest
- * Description:
- * Copyright: Copyright (c) 2010 by tl3shi.
- **/
- Package com. i3zhai. WWW;
- /**
- * Title: nativehelloworld. Java
- * Description: test the native method in the Java keyword.
- * @ Author: <a href = "mailto: tanglei3shi@163.com"> tl3shi </a>
- * @ Date: 10:23:24
- * @ Version 1.0
- */
- Public class nativehelloworld
- {
- /**
- * Tl3shi
- * 10:27:23 pm
- * Discription: constructor to load DLL files.
- * You can also use static to introduce the DLL file.
- * Load the DLL before the program is used.
- */
- Public nativehelloworld ()
- {
- System. loadlibrary ("hello ");
- }
- /**
- * Tl3shi
- * 10:28:24 pm
- * Discription: declares a native method.
- * Because native only declares that the implementation is implemented by another C language such
- */
- Public native void nativehello ();
- /**
- * Tl3shi
- * 10:28:59 pm
- * Discription: The method implemented in C language is called in this class.
- */
- Public void sayhello ()
- {
- Nativehello (); // call Method
- }
- }
Annotations have been written above. At the beginning, I used eclipse directly, but found that the DLL file could not be found later (eclipse estimates where to configure it ). So it is changed to the console.
First, use the javac command to compile the class file. Class files are generated without compilation errors.
Then run the javah command. See javah usage. You can see it in the console.
R:/native> javah
Usage: javah [Option] <class>
[Options] include:
-Help: output the help message and exit.
-Classpath <path> indicates the path used to load the class.
-Bootclasspath <path> is used to mount the path of the boot class.
-D <directory> output directory
-O <File> output file (only one of-D or-O can be used)
-JNI: JNI-style header file (default)
-Version: Output version information.
-Verbose enables detailed output
-Force always writes data to the output file
Use a fully qualified name to specify the <class> (for example, java. Lang. object ).
Directly use javah nativehelloworld to generate a. h file. As follows:
Code:
- /* Do not edit this file-it is machine generated */
- # Include
- /* Header for class nativehelloworld */
- # Ifndef _ included_nativehelloworld
- # DEFINE _ included_nativehelloworld
- # Ifdef _ cplusplus
- Extern "C "{
- # Endif
- /*
- * Class: nativehelloworld
- * Method: nativehello
- * Signature: () V
- */
- Jniexport void jnicall java_nativehelloworld_nativehello
- (Jnienv * XX, jobject XXX );
- # Ifdef _ cplusplus
- }
- # Endif
- # Endif
Then write hello. The C file is as follows:
Code:
- # Include "nativehelloworld. H"
- Jniexport void jnicall java_nativehelloworld_nativehello
- (Jnienv * XX, jobject XXX)
- {
- Printf ("helloworld, I am printing in the C ");
- }
Jniexport void jnicall java_nativehelloworld_nativehello directly copy the header file,
The next step is to generate the DLL file. The DLL file is the name of the parameter to be loaded in the nativehelloworld class. You can use tools such as vc6.0 to generate DLL files. During the build process, problems may occur. The parameter must be added to the above method signature. XX and XXX above. This was added later, not after javah.
During the build process, you can see the error information and use other DLL files. Go to the JDK installation directory and search. JNI. H is available, where JNI. h uses jni_md.h. Directly copy the file to the working path of VC. Then you can find the DLL file in the DEBUG directory.
Now, it's almost done. Write another test.
Code:
- /**
- * Title: nativehelloworld. Java
- * Description: test the native method in the Java keyword.
- * @ Author: <a href = "mailto: tanglei3shi@163.com"> tl3shi </a>
- * @ Date: 10:23:24
- * @ Version 1.0
- */
- Public class test
- {
- /**
- * Tl3shi
- * 10:23:24 pm
- * Discription:
- * @ Param ARGs
- */
- Public static void main (string [] ARGs)
- {
- Nativehelloworld Nat = new nativehelloworld ();
- Nat. sayhello ();
- }
- }
Run javac test. Java test on the console to view the result.