Home>

The first time I met native was a hashcode method in the java.lang.object source code:

public native int hashcode ();

Why is there a native?This is where I want to learn.So I want to summarize the native.

First, recognize native is jni, java native interface

Any languageBoth want to be pure.For example, to solve a certain solution, I like to write in this language.The java platform has an API that the user and the native c code interoperate with, called the java native interface.

Second, call "hello, jni" of c with java

We need to follow the convenient steps to get off work:

1.Create a java class, which contains a native method and loadlibrary method to load the library. The hellonative.java code is as follows:

public class hellonative
{
  static
  {
    system.loadlibrary ("hellonative");
  }
  public static native void sayhello ();
  @suppresswarnings ("static-access")
  public static void main (string [] args)
  {
    new hellonative (). sayhello ();
  }
}

First of all, let's pay attention to the native method. The loading of the library also works later.The native keyword tells the compiler (actually jvm) that the method is defined externally,This refers to c. If you run this code directly, The jvm will tell:"a java exception has occurred." The console output is as follows:

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,The virtual machine says it doesn't know how to find sayhello. Both can be written manually below,Natural bricklayer is used

2. Run javah to get the C declaration header file containing the method.h

Put hellonative.java, simply javac javah, as shown

I got the followinghellonative.h file:

/* do not edit this file-it is machine generated * /
#include<jni.h>
/* header for class hellonative * /
#ifndef _included_hellonative
#define _included_hellonative
#ifdef __cplusplus
extern "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 native method.

Here we simply implement this sayhello method as follows:

#include "hellonative.h"
#include<stdio.h>
jniexport void jnicall java_hellonative_sayhello
{
  printf ("hello, jni");
}

4, generate dll shared library, and then java program load library, call it.

On windows, mingw gcc runs as follows

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 generated dll library is 64-bit. Then run hellonative:

java hellonative

Finally successfully see the console prints as follows:

hello, jni

Three, jni call c flowchart

Fourth, other introductions

Native is used when co-developed with C++! Java does not need to develop itself!

Use the native keyword to indicate that this method is a native function,That is, this method is implemented in the c/c++ language.And it is compiled into a dll and called by java.

The implementation body of these functions is not included in the DLL and jdk source code.You should not see it.They are 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.

1. Native is used for collaboration between java and other languages ​​(such as c++), that is, the implementation of functions after native is not written in java

2. Since it's not java, don't worry about its source code.Ha ha

Native means to notify the operating system,

You have to implement this function for me,Because I want to use.

So the native keyword functions are implemented by the operating system,Java can only be called.

java is a cross-platform language,Since it is cross-platform,The price paid is to sacrifice some control over the bottom,And java needs to achieve the control of the underlying,Need some help in other languages,This is the role of native

java is not perfect,The shortcomings of Java are not only slower than traditional C++, but alsoJava cannot directly access the bottom layer of the operating system (such as system hardware, etc.). To this end, Java uses native methods to extend the functions of Java programs.

You can compare the native method to the interface between a java program and a C program.The implementation steps:

1. Declare the native () method in java, and then compile;

2. Generate one with javah.h file;

3. Write a .cpp file to implement the native export method.It needs to contain the second step.h file (note that it also contains jni.h file with jdk);

4. The third step.The cpp file is compiled into a dynamic link library file;

5. Use the system.loadlibrary () method in Java to load the dynamic link library file generated in step 4.The native () method can be accessed in Java.

Use cases for java native methods

1. In order to use a feature of the underlying host platform,And this feature cannot be accessed through java api

2. To access an old system or use an existing library,And this system or this library is not written in java

3. To speed up the performance of the program,Instead, implement time-sensitive code as a native method.

  • Previous SQL Server trigger and transaction usage examples
  • Next Using Python to implement book overdue reminders