The role of the native keyword in Java

Source: Internet
Author: User

First met native is a hashcode method in the Java.lang.Object source code:

1 public native int hashCode();

Why is there a native? This is the place where I want to study. So the following want to summarize the next native.

Back to the top one, know native is Jni,java native Interface

All a language, all hope is pure. For example, to solve a certain program is like to write in this language alone. Java platform has a user and local C code interoperability API, called Java Native Interface (Java Local interface).

Back to top second, call C's "Hello,jni" with Java

We need to follow the easy steps:

1. Create a Java class that contains a native method and a method to load the library loadLibrary. The Hellonative.java code is as follows:

123456789101112131415 public class HelloNative{    static    {        System.loadLibrary("HelloNative");    }        public static native void sayHello();         @SuppressWarnings("static-access")    public static void main(String[] args)    {        new HelloNative().sayHello();    }}

The first thing to pay attention to is the native method, the load library to the back also work. The native keyword tells the compiler (in fact, the JVM) that the method is called externally, and this refers to C. If you run this code directly, the JVM will tell you: "A Java Exception has occurred." The console output is as follows:

12345 Exception in thread "main" java.lang.UnsatisfiedLinkError: no HelloNative in java.library.path    at java.lang.ClassLoader.loadLibrary(Unknown Source)    at java.lang.Runtime.loadLibrary0(Unknown Source)    at java.lang.System.loadLibrary(Unknown Source)    at HelloNative.<clinit>(HelloNative.java:5)

This is when the program uses it, and the virtual machine says it doesn't know how to find SayHello. The following can be written manually, natural masons are used

2. Run Javahto get the C Declaration header file containing the method . h

Will Hellonative.java, simply Javac Javah,

You get the following HelloNative.h file :

123456789101112131415161718192021 /* DO NOT EDIT THIS FILE - it is machine generated */#include <jni.h>/* Header for class HelloNative */#ifndef _Included_HelloNative#define _Included_HelloNative#ifdef __cplusplusextern "C" {#endif/* * Class:     HelloNative * Method:    sayHello * Signature: ()V */JNIEXPORT void JNICALL Java_HelloNative_sayHello  (JNIEnv *, jclass);#ifdef __cplusplus}#endif#endif

Jni.h this file, in/%java_home%include

3, according to the header file, write c to implement the local method .

Here we simply implement this SayHello method as follows:

1234567 #include "HelloNative.h"#include <stdio.h>JNIEXPORT void JNICALL Java_HelloNative_sayHello{    printf("Hello,JNI");    }

4, build the DLL shared library , and then Java Program Load Library, call can.

On Windows, MinGW GCC runs as follows

1 gcc -m64  -Wl,--add-stdcall-alias -I"C:\Program Files\Java\jdk1.7.0_71\include" -I"C:\Program Files\Java\jdk1.7.0_71\include\include\win32" -shared -o HelloNative.dll HelloNative.c

-m64 indicates that the build DLL library is 64-bit. Then run hellonative:

1 java HelloNative

Finally, you can see the console print as follows:

1 Hello,JNI
Back to top third, JNI call C flowchart

Back to top four, other introduction

Native is used in conjunction with C + + development! Java self-development is not necessary!


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.


1. Native is used in collaboration with Java and other languages such as C + +.
The implementation of the function after native is not written in Java.
2. Since it is not Java, then forget its source code, hehe


Native means to notify the operating system,
This function you must give me to implement as I want to use.
So the functions of the native keyword are all implemented by the operating system,
Java can only be called.


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

Java is not perfect, Java is not enough to run faster than the traditional C + + slower, Java can not directly access to the operating system (such as system hardware), for this Java use the native method to extend the functionality of Java programs.
The native method can be likened to the Java program's interface to the C program, and its implementation steps:
1, declare the native () method in Java, and then compile;
2. Create an. h file with Javah;
3, write a. cpp file to implement the native export method, which needs to include the second step produced by the. h file (Note that it also contains the JDK jni.h file);
4, the third step of the. cpp file is compiled into a dynamic link library file;
5, in Java with the System.loadlibrary () method to load the fourth step generated by the dynamic link library file, the native () method can be accessed in Java.


Scenarios where Java native methods are applicable
1. In order to use one of the features of the underlying host platform, this feature cannot be accessed through the Java API

2. In order to access an old system or use an existing library, this system or library is not written in Java

3. To speed up the performance of the program, a period of time-sensitive code is implemented as a local method.

The role of the native keyword in Java

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.