Java basics: Understanding & amp; Understanding of the keyword native practice, java Basics

Source: Internet
Author: User

Java basics: Understanding and understanding of native keywords

Writer: BYSocket)

Weibo: BYSocket

Douban: BYSocket

Navicat is a hashCode method in the source code of java. lang. Object:

?
1 public native int hashCode();

Why is there a navicat? This is what I want to learn. So today, The bricklayer wants to summarize navicat.

1. Understand native, namely JNI and Java Native Interface

All languages must be pure. For example, a solution can be written in this language alone. The Java platform has an API that allows users to interoperate with local C code. It is called Java Native Interface (Java Local Interface ).

2. Use Java to call C's "Hello, JNI"

We need to follow the convenient steps after work:

1. Create a Java class that contains a native method and the loadLibrary Method for loading the library. 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();    }}

First of all, the bricklayer pays attention to the native method, and the loading of the database also works. The native keyword tells the compiler (actually the JVM) to call the method in an external definition. Here it refers to C. If you directly run this code, 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)

When the program uses it, the virtual machine says it does not know how to find sayHello. Which of the following can be manually written?

2. Run javah to obtain the C Declaration header file. h containing the method.

The bricklayer simply uses HelloNative. java to javac javah,

The following HelloNative. h file is obtained:

?
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 file in/% JAVA_HOME % include

3. Write C as the header file to implement the local method.

Here we can simply implement this sayHello method as follows:

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

4. Generate the dll shared library, load the library of the Java program, and call it.

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

?
1 java HelloNative

Finally, the console is displayed as follows:

?
1 Hello,JNI
Iii. flowchart of JNI Calling C

Writer: BYSocket)

Weibo: BYSocket

Douban: BYSocket

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.