Java native methods and examples of JNI

Source: Internet
Author: User
Tags visual studio 2010

1. Reference documents:

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: Invoke C or C + + dynamic Join library with JNI so simple

Summary of technical practice of reference 2:jni

Reference 3:jni Simple Example

2. Overview

Today in the Java multithreaded programming, found that thread this class has a number of native methods, have never seen this method before, so for the more curious, look up some information, now tidy up, to make memo.

2.1.native keyword Usage

Native is used in conjunction with C + + development! using the native keyword means that this method is a native function, that is, the method is implemented in C/s + + language and is compiled into a DLL, which is called by Java . The implementation of these functions in the DLL, the JDK source code is not included, you should be invisible. They are also different for different platforms. This is also the underlying mechanism of Java, in fact, Java is on different platforms with different native methods to achieve access to the operating system. Word:

    1. Native is used in collaboration with Java and other languages such as C + +, which means that the implementation of functions after native is not written in Java.
    2. Since it is not Java, then forget the source code, we just need to know that this method has been implemented.
    3. Native means to inform the operating system that this function you must give me to implement as I want to use. So the function of the native keyword is implemented by the operating system, and Java can only be called.
    4. Java is a cross-platform language, since it is cross-platform, the cost is to sacrifice some of the underlying control, and Java to achieve the control of the underlying, need some other language help, this is the role of native
2.2JNI Introduction The native method is implemented through JNI in Java. JNI is the abbreviation for Java Native interface. Beginning with Java 1.1, the Java Native Interface (JNI) standard became part of the Java platform, allowing Java code to interact with code written in other languages. JNI was initially designed for local compiled languages, especially C and C + +, but it does not prevent you from using other languages, as long as the calling convention is supported. Using Java to interact with locally compiled code often loses platform portability. However, in some cases this is acceptable and even necessary, such as using some old libraries, interacting with the hardware, the operating system, or to improve the performance of the program. The JNI standard guarantees at least local code to work with any Java virtual machine implementation.

There are 3 main technologies for Java to interact with DLLs: Jni,jawin and Jacob. Jni (Java Native Interface) is a technique that sun provides to interact with native methods in the system (Java and Native method intermodulation in the Windows\linux system). It can only be implemented by C + + at this time. The latter two are open-source projects on SourceForge and are also an application repository on the Windows system based on JNI technology. Jacob (java-com Bridge) provides the ability of Java programs to invoke methods in Microsoft's Com objects. In addition to COM objects, Jawin (JAVA/WIN32 integration project) can also win32-dll methods in a dynamic-link library. in terms of functionality: JNI >> Jawin>jacob, with its approximate structure such as:

in terms of ease of use, it's the opposite: Jacob>jawin>>jni.

While the JVM encapsulates the actual variability of the various operating systems, it provides JNI technology that enables developers to invoke the library functions implemented by the operating system-related technologies through Java Programs (code), thus interacting with other technologies and systems, and using other technologies to implement the functions of the system At the same time, other technologies and systems can also invoke the functions implemented within the Java application system via the corresponding native interfaces provided by JNI.

On Windows systems, generally executable applications are based on the native PE structure, and the JVM on Windows is based on the native architecture. Java applications are built on top of the JVM.

JNI can be seen as a proxy mode for the application itself. For developers, it is necessary to implement an agent (JNI program) to actually manipulate the target native function using C/s + +, in Java program The JVM calls the target native function indirectly by loading and invoking this JNI program.


Writing steps for 2.3JN
    1. Write a Java class with a method of native declaration to generate a. java file
    2. Compile the Java class written using the Javac command to generate a. class file
    3. Generates a. h file by using the Javah-jni Java class name to generate a header file with the extension H.
    4. Implement the Local method using C + + (or other programming-like language), create an implementation of the. h file, that is, create a. cpp file to implement the method in the. h file
    5. Generate a DLL file by generating a dynamic connection library of files written in C + +
3.JNI instance the following is where all operations are in the directory: D:\JNIThe benefits of doing so are easy to control. Another requirement is that our Java class does not contain a package name, and currently I only test the type of success without package name. 3.1. Write the Java class with the native declaration method: Helloworld.java [Java]View Plaincopy
  1. Public class HelloWorld {
  2. public native void Displayhelloworld (); Java Native Method declaration
  3. Static {
  4. System.loadlibrary ("Helloworldimpl");   Load the dynamic-link library, and "Helloworldimpl" is the name of the dynamic-link library to mount.
  5. }
  6. public static void Main (string[] args) {
  7. //TODO auto-generated method stub
  8. HelloWorld HelloWorld = new HelloWorld ();
  9. Helloworld.displayhelloworld ();
  10. }
  11. }
3.2. Compiling the Java class written using the Javac command [Java]View Plaincopy
    1. D:\jni>javac Helloworld.java
Generated after executing the above command D:\JNI\HelloWorld.classFile 3.3. Using Javah-jni Java class name to generate a header file with the extension h [Java]View Plaincopy
    1. D:\jni>javah-jni HelloWorld
Generated after executing the above command D:\JNI\HelloWorld.hFile, the contents of which are as follows: [Java]View Plaincopy
  1. /* Don't EDIT this file-it are machine generated */
  2. #include <jni.h>
  3. /* Header for class HelloWorld */
  4. #ifndef _included_helloworld
  5. #define _included_helloworld
  6. #ifdef __cplusplus
  7. extern "C" {
  8. #endif
  9. /*
  10. * Class:helloworld
  11. * Method:displayhelloworld
  12. * Signature: () V
  13. */
  14. Jniexport void Jnicall Java_helloworld_displayhelloworld
  15. (JNIENV *, jobject);
  16. #ifdef __cplusplus
  17. }
  18. #endif
  19. #endif
Here we can understand: This h file is equivalent to our interface in Java, here declares a java_helloworld_displayhelloworld (jnienv *, jobject); Then we implement this method in our native method, that is, we must use the same method name as the one we used to write the C + + program 3.4. Create a HelloWorldImpl.cpp using the C + + implementation Local method, as shown in the code below: [Java]View Plaincopy
  1. #include "HelloWorld.h"
  2. #include <stdio.h>
  3. #include <jni.h>
  4. /*
  5. * Class:helloworld
  6. * Method:displayhelloworld
  7. * Signature: () V
  8. */
  9. Jniexport void Jnicall Java_helloworld_displayhelloworld
  10. (JNIENV *, Jobject)
  11. {
  12. printf ("Hello world!\n");
  13. return;
  14. }
3.5. Generating a dynamic connection library for a file written by C + + will D:\Program files\java\jdk1.6.0_26\include\jni.hAnd D:\Program files\java\jdk1.6.0_26\include\win32\jni_md.hThese two files are copied to D:\JNI\Directory. Same directory as HelloWorldImpl.cpp, directory structure as shown: 3.7 Execute cl/ld D:\JNI\HelloWorldImpl.cpp get HelloWorldImpl.dll file I'm using Visual Studio 2010, to use the CL command, you must open the Visual Studio command line as shown in: then enter the following command on the command line [Java]View Plaincopy
    1. Cl/ld D:\JNI\HelloWorldImpl.cpp
As shown in the following examples:
After executing the above command, we can see the generated four files in C:\Program files (x86) \microsoft Visual Studio 10.0\VC, respectively:
    • HelloWorldImpl.dll
    • Helloworldimpl.exp
    • HelloWorldImpl.lib
    • Helloworldimpl.obj
Copy one of the HelloWorldImpl.dll to D:\JNI\Directory. 3.8. Execute class to get the result in cmd run: [Java]View Plaincopy
    1. D:\jni>java HelloWorld
As shown in: 4. Running under Eclipse
    • 4.1 Create a project called Jnitest under Eclipse
    • 4.2 Add a Helloworld.java of the same as 3.1
    • 4.3 After saving Helloworld.java, Helloworld.class is generated in the Jnitest\bin directory.
    • 4.4 Generate HelloWorld.h files based on Helloworld.class
    • 4.5 creating HelloWorldImpl.cpp to implement methods in HelloWorld.h
    • 4.6 Build HelloWorldImpl.dll using Visual Studio 2010
    • 4.7 Run the HelloWorld program in eclipse with the following error:
[Java]View Plaincopy Method 1 in the project---> Properties--->java build path----> select your project name---> Configure the DLL path in native library location into Method 2. Propeties for src---->native Library---The DLL path is configured in the >location path
    1. Java.lang.UnsatisfiedLinkError:no Helloworldimpl in Java.library.path
    2. At Java.lang.ClassLoader.loadLibrary (Classloader.java:1738)
    3. At Java.lang.Runtime.loadLibrary0 (Runtime.java:823)
    4. At Java.lang.System.loadLibrary (System.java:1028)
    5. At Helloworld.<clinit> (Helloworld.java:6)
    • 4.8 Copy the HelloWorldImpl.dll to the C:\Windows\System32
    • 4.9 Execute the HelloWorld program again, the program runs normally, the console output "Hello world!

Java native methods and examples of JNI

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.