Java Fundamentals: Understanding & Understanding Keywords Native combat

Source: Internet
Author: User

Writer:bysocket (mud and brick pulp carpenter)

Weibo: Bysocket

Watercress: Bysocket

Mason first met Navicat is a hashcode method in the Java.lang.Object source code:

1 public native int hashCode();

Why is there a navicat? This is the place where I want to study. So today Masons want to summarize the next navicat.

First, the understanding native namely 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).

Second, using Java to call C's "Hello,jni"

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:

1 2 3 4 5 6 7 8 9 Ten, public class hellonative { &n bsp;    static      {          system.loadlibrary ("hellonative");      }             Public static native void SayHello ();             @SuppressWarnings (" Static-access ")      public static void Main (string[] args)      {          new Hellonative (). SayHello ();      } }

     First Masons let everyone notice is the native method, the loading 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:

1 2 3 4 5 EXC Eption in thread "main" Java.lang.UnsatisfiedLinkError:no hellonative in Java.library.path   & nbsp;  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

The Masons will Hellonative.java, simply Javac Javah,

    got the following   HelloNative.h file   :

1 2 3 4 5 6 7 8 9 Ten each of them /* Do not EDIT the this FILE -It is machine generated */ #include < jni.h ; /* Headers for CL 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 local method .

Here we simply implement this SayHello method as follows:

1 2 3 4 5 6 7 #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
Third, JNI call C flowchart

Writer:bysocket (mud and brick pulp carpenter)

Weibo: Bysocket

Watercress: Bysocket

Java Fundamentals: Understanding & Understanding Keywords Native combat

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.