Call DLL (SO) (on) in JNI Mode)

Source: Internet
Author: User

Recently there was a project to call the original DLL file, which was originally intended to use JNA, but the customer did not agree that JNI should be used. In desperation, JNI can only be used again. The general method of calling dynamic link library in Java is summarized as follows.

JNI is short for Java Native Interface (Java local call. Java Native Interface (JNI) standard has become part of the Java platform since Java 1.1.CodeInteract with code written in other languages. JNI was initially designed for locally compiled languages, especially C and C ++, but it does not prevent you from using other languages, as long as the call conventions are supported.

To call a DLL file using JNI, follow these steps:

1. Compile a Java class with native declarations

2. Use the javac command to compile the compiled Java class

3. Use the javah command to compile the file into a header file

4. Use C/C ++ to implement local methods (including header files generated in step 1)

5. Generate a dynamic connection library for files written in C/C ++

The following describes the specific implementation of the above five steps through examples.

-> Compile a Java class with native declarationsThe Code is as follows:

View code

 1   Package  Com. wanggc. JNI;  2   3   Public   Class  Myjninativemethods {  4       /**  5   * The maximum value is used.  6  *  7   *  @ Param  Data1  8   * Data 1  9   *  @ Param  Data2  10   * Data 2  11   *  @ Return  12       */  13       Public   Native   Int Getmaxdata ( Int Data1, Int  Data2 );  14   15       /**  16   * Say hello to someone.  17   *  18   * @ Param  Name  19   * Person name  20   *  @ Return  21        */  22       Public   Native  String getgreeting (string name );  23   24       /**  25  * Load the dynamic link library  26        */  27       Static  {  28 System. loadlibrary ("testjni" );  29   }  30   31       /**  32   * Main function of the portal.  33   * 34   *  @ Param  ARGs  35        */  36       Public   Static   Void  Main (string [] ARGs ){  37 Myjninativemethods jnitest = New  Myjninativemethods ();  38           // Call the first local method to obtain the maximum value of the two numbers.  39           Int Maxdata = jnitest. getmaxdata (100,150 );  40   System. Out. println (maxdata );  41   42           //  Call the second local method and ask someone to say hello.  43 String Hello = jnitest. getgreeting ("Wang guocheng" );  44   System. Out. println (Hello ); 45   }  46 }

Row 13 and row 22 contain two local methods: getmaxdata and getgreeting. Note that the local method only requires the method name and does not require the method body. The native keyword must be used for modification. Lines 27-29 are used to load dynamic link libraries. Note that no suffix is required for the DLL file, as long as the name is enough.

-> Use the javac command to compile the compiled Java classIf the code is written in eclipse, you do not need to use this command. Use the clean option of the Project menu to compile it.

-> Use the javah command to compile the file into a header file

After compiling the class file, open the CMD window, go to the bin directory, and run

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.