Implementation of Java JNI-further deepening understanding of JVM implementations

Source: Internet
Author: User
Tags function prototype

Directory

Overview

Key Benefits

Main disadvantages

A simple example of JNI implementations

Development tools

Brief steps

1, declare a ' native method' in Eclipse's ' Java class ';

2, use the ' javah ' command to generate a ' C + + header filecontaining ' native method ' definition;

3, the use of ' C + + ' to achieve the above generation of the ' C + + header file ' method;

4, compile the implemented file as a '. dll ' dynamic link library ;

5. In eclipse, add the path to the '. dll dynamic link library ' to ' classpath 'in Eclipse;

6. load. dll in the Java class in eclipse and call the native method .

Specific steps

1.

2.

3.

4.

5.

6.

Overview

In fact, careful programmer should note that Java source code, the implementation of the method, either concrete implementation, or is defined as native. The method defined as native is given to languages such as C and implemented on specific operating systems. We know that Java runs through the JVM, Many of the JVMs are implemented through C and assembler. We can guess, specifically, through the powerful sun-provided JNI, the bridge to achieve.

We also know why each operating system, corresponding to a different JVM, is actually the implementation of JNI is caused by non-cross-platform.

Some of the processes involved in JNI invocation are generally understood as a point.

Key Benefits:

      1. The Java language calls implementations of other languages, such as C + +. This is sometimes really necessary, such as Java needs to call a method (the algorithm is difficult, but it is written in a non-Java language, and you are very anxious, if you want to implement in Java, it will cost a lot of development costs).
      2. High performance, this I did not quite understand at first, in fact, the implementation of JNI, can use the old library, some rely on the machine language exchange, which naturally improve performance.

Main disadvantages:

      1. From the 2nd above, the obvious drawback is that when we use JNI for direct interaction with the operating system, or rely on machines or hardware, the natural portability is lost. So Jnij is part of Ava, but to be cautious, compared, Java is famous for its cross-platform. That is, If this program (JNI-based) requires cross-platform, the native method declaration has an implementation that needs to be re-modified (this is a cost of development costs).
      2. One advantage of using JNI is that Java is a strongly typed language and is a compiled language, and C + + is not, so you need to be very careful when developing, after all, it is non-secure to manipulate memory between languages such as C + +.

A simple example of JNI implementations

Here's how to use a simple JNI program to further understand JNI:

Development tools:

1,JDK v1.7.0_71 (x64);

2,eclipse Java EE IDE for Web developers (Indigo Release), or Eclipse v3.7.0;

3,visual Studio Express (x86);

4, my operating system is Win7 flagship version SP1 (x64).

Brief steps:

1, declare a ' native method' in Eclipse's ' Java class ';

2, use the ' javah ' command to generate a ' C + + header filecontaining ' native method ' definition;

3, the use of ' C + + ' to achieve the above generation of the ' C + + header file ' method;

4, compile the implemented file as a '. dll ' dynamic link library ;

5. In eclipse, add the path to the '. dll dynamic link library ' to ' classpath 'in Eclipse;

6. load. dll in the Java class in eclipse and call the native method .

Specific steps:

1, create a new Java Project named ' SayHello ' in Eclipse and add a class with a full name of ' Juk.demo.SayHello ', with the following code:

1  PackageJuk.demo;2 3  Public classSayHello {4 5      Public  native voidSayHello ();6     7      Public Static voidMain (string[] args) {8         9     }Ten  One}
View Code

2, open the command-line window through ' cmd ', go to ' bin ' directory under ' SayHello ' root directory, use ' javah juk.demo.SayHello ' command to generate ' C + + header file' containing ' native method ' definition;

3,a, open VS2013, open the New Project dialog box with ' File->new project ', select ' templates/visual c++/win32 ' on the left, and select ' Win32 Project ', enter the name ' Cppsay ' The project name, select the appropriate path, OK. B, under the ' Solution Explore ' view, right-click on the ' Cppsay ' project, select ' Open Folder in file Explore ' and put the ' C/R + + header file generated by the first step above into the ' Cppsay.vcxproj ' in the same category as the ' include\jni.h ' and ' include\win32\jni_md.h ' in the JDK installation directory (for example ' H:\Program files\java\ Jdk1.7.0_71\include\jni.h ' and ' H:\Program files\java\jdk1.7.0_71\include\win32 '), added to the same directory. C, right-click on ' header Files ' in ' Solution Explore ' view and select ' Add->existing Item ' to introduce the 3. h header files just added. ' Source ' in ' Solution Explore ' view Files ' Add a '. cpp file to implement the Java-defined native method (through machine-generated function prototypes), the code is:

1#include <iostream>2#include"juk_demo_sayhello.h"3 using namespacestd;4 5 //(the function prototype in "Juk_demo_sayhello.h") implements the function of the header file produced by the Javah (or the native method of implementing Java definitions in C + +).6JniexportvoidJnicall Java_juk_demo_sayhello_sayhello7(JNIENV *env, Jobject obj) {8cout<<"Hello, Java Native interface"<<Endl;9}
View Code

4, change the ' solution Platform ' in the VS2013 ' standard ' toolbar to ' x64 ' (because my JDK is x64), or you will get an error when loading the DLL in Java:can ' t load IA 32-bit. dll On a AMD 64-bit platform, and then compile the entire project, will generate ' CppSay.dll ' dynamic link libraries under the Debug directory in the ' x64 ' directory of the ' solution ' (note that the debug under the solution, not the debug under the project), Catalogs such as ' C:\Users\Administrator\Documents\visual studio 2012\projects\cppsay\x64\debug '

5, right-click on the ' JRE System Library ' under Eclipse's ' SayHello ' project, select ' Build path ', ' Config build Path', select ' Java build ' the ' Native Library location ' in the ' JRE System library ' under Path ', click on the ' Edit ' button on the right to select the ' CppSay.dll ' directory above the 4th step, The configuration of the classpath is completed.

6, in the 1th step of the Java class, define a static code block, using:

System.loadlibrary ("Cppsay")

Load the dynamic link library (or not through a static block of code), note that the name cannot contain '. dll ', or it will error: Java.lang.UnsatisfiedLinkError:no CppSay.dll in Java.library.path, the ' SayHello ' project running Eclipse will output on the console: ' Hello, Java Native interface '.

This is the simple use of JNI.  

        

Implementation of Java JNI-further deepening understanding of JVM implementations

Related Article

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.