Java Native method and JNI instance

Source: Internet
Author: User
ArticleDirectory
    • 2.1.native keyword usage
    • 2.2jni Introduction
    • 2.3jn writing steps
    • . Compile a Java class with native Declaration: helloworld. Java
    • . Use the javac command to compile the compiled Java class
    • 3. Use javah-JNI Java class name to generate a header file with the extension H
    • 3. 4. Use C/C ++ to implement local methods
    • . Generate a dynamic connection library for files written in C/C ++
    • 3.7 run Cl/ld d: \ JNI \ helloworldimpl. cpp to obtain the helloworldimpl. dll file.
    • . Execute the class to obtain the result.
1. References:

Http://blog.csdn.net/youjianbo_han_87/article/details/2586375

Http://blog.csdn.net/yangjiali014/article/details/1633017

Http://blog.chinaunix.net/space.php? Uid = 7437948 & Do = Blog & id = 2054823

Http://www.iteye.com/topic/72543

Http://www.enet.com.cn/article/2007/1029/A20071029886398.shtml

Http://blog.csdn.net/heqingrong623/article/details/3906350

Reference 1: calling the C or C ++ dynamic connection library with JNI is so simple.

Reference 2: JNI technical practice summary

Reference 3: simple JNI instance

2. Overview

Today, when I was looking at Java multi-threaded programming, I found that there are multiple native methods in the Thread class. I have never seen this method before. So I am curious about it. I have read some materials. Now I have sorted it out, for memo.

2.1.native keyword usage

Native is used for joint development with C ++!The native keyword indicates that this method is a native function, that is, this method is implemented in C/C ++ and compiled into a DLL, which is called by Java.. These functions are implemented in the DLL.Source codeIs not included, you should not be able to see. They are also different for different platforms. This is also the underlying mechanism of Java. In fact, Java calls different native methods on different platforms to access the operating system. All in all:

    1. Native is used for collaboration between Java and other languages (such as C ++), that is, the implementation of native functions is not written in Java.
    2. Since it is not Java, leave it alone.CodeNow, we only need to know that this method has been implemented.
    3. Native means to notify the operating system. You must implement this function for me because I want to use it. Therefore, functions with native keywords are implemented by the operating system and can only be called in Java.
    4. Java is a cross-platform language. Since it is a cross-platform language, the cost is to sacrifice some control over the underlying layer, while Java must implement control over the underlying layer, we need help from other languages. This is the role of native.
2.2jni introduction native methods are implemented through JNI in Java. JNI is short for Java Native Interface. Since Java 1.1, the Java Native Interface (JNI) standard has become part of the Java platform. It allows Java code to interact 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. Using Java to interact with locally compiled code usually results in loss of platform portability. However, in some cases, this is acceptable or even necessary. For example, you can use old libraries to interact with hardware and operating systems, or Program Performance. The JNI standard should at least ensure that local code can work in any Java
Virtual Machine implementation.

Currently, Java and DLL interaction technologies mainly include JNI, jawin, and Jacob.JNI (Java Native Interface) is a technology provided by Sun for Java to interact with native methods in the system (in Windows \ Linux systems, Java and native method can be intertuned ). Currently, it can only be implemented by C/C ++. The last two are both open-source projects on SourceForge and application libraries on Windows systems based on JNI technology. Jacob (Java-com Bridge) provides the ability for Java programs to call methods in Microsoft's COM object. Besides the COM Object, jawin (Java/Win32
Integration Project) You can also win32-dll methods in the dynamic link library.In terms of functions: JNI> jawin> Jacob, its general structure is as follows:

In terms of usability, the opposite is true: Jacob> jawin> JNI.

JVM encapsulates the differences between various operating systems and provides JNI technology so that developers can use Java programs (CODE) to call the library functions implemented by operating system-related technologies, to interact with other technologies and systems, and use other technologies to implement system functions; at the same time, other technologies and systems can use the corresponding native interfaces provided by JNI to call functions implemented inside the Java application system.

In Windows, executable applications are generally based on the native PE structure, and the JVM on Windows is also implemented based on the native structure. Java application systems are built on JVM.

JNI can be seen as a proxy mode for the application itself. Developers need to use C/C ++ to implement a proxy Program (JNI program) to actually operate the target native function, in Java programs, the JVM indirectly calls the target native function by loading and calling the JNI program.

2.3jn writing steps
    1. Compile a Java class with native declarations to generate a. Java file.
    2. Use the javac command to compile the compiled Java class and generate the. Class file.
    3. Use javah-JNI JavaClass NameGenerate a header file with the extension of H, that is, generate a. h file.
    4. Use C/C ++ (or other programming languages) to implement local methods and create. h file implementations, that is, the methods in the. cpp file implementation. h file.
    5. Generate dynamic Connection Library and DLL file for files written in C/C ++
3. The following operations on the JNI instance are in the directory: D: \ JNI To facilitate control. Another requirement is that our Java class does not contain the package name. Currently, I only test the type that is successful and does not contain the package name. . Compile a Java class with native Declaration: helloworld. Java <textarea readonly name="code" class="java">Public class helloworld {<br/> Public native void displayhelloworld (); // Java Native method declaration </P> <p> static {<br/> system. loadlibrary ("helloworldimpl"); // load the dynamic link library. "helloworldimpl" is the name of the dynamic link library to be loaded. <Br/>}</P> <p> Public static void main (string [] ARGs) {<br/> // todo auto-generated method stub <br/> helloworld = new helloworld (); <br/> helloworld. displayhelloworld (); <br/>}< br/>}</textarea>
. Use the javac command to compile the compiled Java class

<textarea readonly name="code" class="java">D: \ JNI> javac helloworld. Java</textarea> Generate D: \ JNI \ helloworld. Class File 3. 3. Use the javah-JNI Java class name to generate a header file with the extension H<textarea readonly name="code" class="java">D: \ JNI> javah-JNI helloworld</textarea> Generate D: \ JNI \ helloworld. h File. The content of this file is as follows: <textarea readonly name="code" class="java">/* Do not edit this file-it is machine generated */<br/> # include <JNI. h> <br/>/* Header for class helloworld */</P> <p> # ifndef _ included_helloworld <br/> # DEFINE _ included_helloworld <br/> # ifdef _ _ cplusplus <br/> extern "C" {<br/> # endif <br/>/* <br/> * class: helloworld <br/> * method: displayhelloworld <br/> * Signature :() v <br/> */<br/> jniexport void jnicall java_helloworld_displayhelloworld <br/> (jnienv *, jobject ); </P> <p> # ifdef _ cplusplus <br/>}< br/> # endif <br/></textarea> Here we can understand as follows: This H file is equivalent to our interface in Java. Here we declare a java_helloworld_displayhelloworld (jnienv *, jobject); method, then implement this method in our local method, that is to say, the method name we use when writing the C/C ++ program must be consistent with the method name here 3. 4. use C/C ++ to create helloworldimpl locally. CPP, the Code is as follows: <textarea readonly name="code" class="java"># Include "helloworld. H "<br/> # include <stdio. h> <br/> # include <JNI. h> <br/>/* <br/> * Class: helloworld <br/> * method: displayhelloworld <br/> * Signature :() v <br/> */<br/> jniexport void jnicall java_helloworld_displayhelloworld <br/> (jnienv *, jobject) <br/>{< br/> printf ("Hello world! \ N "); <br/> return; <br/>}</textarea>
3. 5. Generate a dynamic Connection Library for the files written in C/C ++ D: \ Program Files \ Java \ jdk1.6.0 _ 26 \ include \ JNI. h And D: \ Program Files \ Java \ jdk1.6.0 _ 26 \ include \ Win32 \ jni_md.h Copy the two files D: \ JNI \ Directory. And helloworldimpl. CPP is in the same directory. The directory structure is shown in: 3.7 execute Cl/ld d: \ JNI \ helloworldimpl. CPP to get helloworldimpl. in the DLL file, I use Visual Studio 2010. To use the CL command, you must open the Visual Studio command line, as shown in. Then, enter the following command in the command line: <textarea readonly name="code" class="java">CL/ld d: \ JNI \ helloworldimpl. cpp</textarea> As shown in:
After executing the preceding command, we can see the four files generated in c: \ Program Files (x86) \ Microsoft Visual Studio 10.0 \ Vc:

    • Helloworldimpl. dll
    • Helloworldimpl. Exp
    • Helloworldimpl. Lib
    • Helloworldimpl. OBJ
Copy helloworldimpl. DLL D: \ JNI \ Directory. . Execute the class and run the following command in cmd: <textarea readonly name="code" class="java">D: \ JNI> JAVA helloworld</textarea> As shown in: 4. Run in eclipse
    • 4.1 create a project named jnitest in eclipse
    • 4.2 Add the same helloworld. Java as 3.1
    • 4.3 After helloworld. Java is saved, helloworld. class will be generated in the jnitest \ bin directory.
    • 4.4 generate helloworld. h file based on helloworld. Class
    • 4.5 create helloworldimpl. cpp to implement the methods in helloworld. h.
    • 4.6 use Visual Studio 2010 to generate helloworldimpl. dll
    • 4.7 run the helloworld program in eclipse. The error is as follows:
<textarea readonly name="code" class="java">Java. lang. unsatisfiedlinkerror: No helloworldimpl in Java. library. path <br/> at java. lang. classloader. loadlibrary (classloader. java: 1738) <br/> at java. lang. runtime. loadlibrary0 (runtime. java: 823) <br/> at java. lang. system. loadlibrary (system. java: 1028) <br/> at helloworld. <clinit> (helloworld. java: 6)</textarea>
    • 4.8 copy helloworldimpl. DLL to c: \ windows \ system32
    • 4.9 run the helloworld program again. The program runs normally and the console outputs "Hello World !"

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.