[Original] Java Native example

Source: Internet
Author: User

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:
  1. /**
     
  2. * Project_name: nativetest
     
  3. * Description:
     
  4. * Copyright: Copyright (c) 2010 by tl3shi.
     
  5. **/
  6. Package com. i3zhai. WWW;
  7. /**
     
  8. * Title: nativehelloworld. Java
     
  9. * Description: test the native method in the Java keyword.
     
  10. * @ Author: <a href = "mailto: tanglei3shi@163.com"> tl3shi </a>
     
  11. * @ Date: 10:23:24
     
  12. * @ Version 1.0
     
  13. */
  14. Public class nativehelloworld
  15. {
  16. /**
     
  17. * Tl3shi
     
  18. * 10:27:23 pm
     
  19. * Discription: constructor to load DLL files.
     
  20. * You can also use static to introduce the DLL file.
     
  21. * Load the DLL before the program is used.
     
  22. */
  23. Public nativehelloworld ()
  24. {
  25. System. loadlibrary ("hello ");
  26. }

  27. /**
     
  28. * Tl3shi
     
  29. * 10:28:24 pm
     
  30. * Discription: declares a native method.
     
  31. * Because native only declares that the implementation is implemented by another C language such
     
  32. */
  33. Public native void nativehello ();
  34. /**
     
  35. * Tl3shi
     
  36. * 10:28:59 pm
     
  37. * Discription: The method implemented in C language is called in this class.
     
  38. */
  39. Public void sayhello ()
  40. {
  41. Nativehello (); // call Method
  42. }
  43. }

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:
  1. /* Do not edit this file-it is machine generated */
  2. # Include
  3. /* Header for class nativehelloworld */
  4. # Ifndef _ included_nativehelloworld
  5. # DEFINE _ included_nativehelloworld
  6. # Ifdef _ cplusplus
  7. Extern "C "{
  8. # Endif
  9. /*
     
  10. * Class: nativehelloworld
     
  11. * Method: nativehello
     
  12. * Signature: () V
     
  13. */
  14. Jniexport void jnicall java_nativehelloworld_nativehello
  15. (Jnienv * XX, jobject XXX );
  16. # Ifdef _ cplusplus
  17. }
  18. # Endif
  19. # Endif

Then write hello. The C file is as follows:

Code:
  1. # Include "nativehelloworld. H"
  2. Jniexport void jnicall java_nativehelloworld_nativehello
  3. (Jnienv * XX, jobject XXX)
  4. {
  5. Printf ("helloworld, I am printing in the C ");
  6. }

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:
  1. /**
     
  2. * Title: nativehelloworld. Java
     
  3. * Description: test the native method in the Java keyword.
     
  4. * @ Author: <a href = "mailto: tanglei3shi@163.com"> tl3shi </a>
     
  5. * @ Date: 10:23:24
     
  6. * @ Version 1.0
     
  7. */
  8. Public class test
  9. {
  10. /**
     
  11. * Tl3shi
     
  12. * 10:23:24 pm
     
  13. * Discription:
     
  14. * @ Param ARGs
     
  15. */
  16. Public static void main (string [] ARGs)
  17. {
  18. Nativehelloworld Nat = new nativehelloworld ();
  19. Nat. sayhello ();
  20. }
  21. }

Run javac test. Java test on the console to view the result.

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.