JNI Small Test Sledgehammer

Source: Internet
Author: User

Jni:

JNI is the abbreviation for Java Native Interface, which provides a number of APIs that enable the communication of Java and other languages (primarily c&c++). Starting with Java1.1, the JNI standard becomes part of the Java platform, which allows Java code to interact with code written in other languages. JNI was initially designed for local compiled languages, especially C and C + +, but it does not prevent you from using other programming languages, as long as the calling convention is supported. Using Java to interact with locally compiled code often loses platform portability. However, in some cases it is acceptable and even necessary. For example, use some old libraries to interact with the hardware, the operating system, or to improve the performance of the program. The JNI standard should at least ensure that local code can work in any Java Virtual machine environment.

Application:

First:

Write the class, and add the native method, not life does not describe the specific implementation.

Ex

1  PackageKg.tom;2  Public classMyjni {3 4     Public  native void display (); 5      Public native double sum (double x,double y);6       7 8      Static {9System.loadlibrary ("Sum");Ten         } One       A  -      Public Static voidMain (string[] args) { -  the          NewMyjni (). display (); -System.out.println (NewMyjni (). SUM (2.0, 3.0)); -     -         +     } -       +       A}

Second:

First, the Javac command compiles the class file to generate the classes byte code, and the Javah command generates the. h file from the class file.

Ex

Third:

Write the native C file.

//Required header file jni.h#include <jni.h>//If you do not set the path of the jni.h to path, you can copy it to the same directory as the C file and modify it to #include "jni.h"//Import the local methods we need to implement#include "kg_tom_myjni.h"//need to be in the C file directory#include<stdio.h>Jniexportvoidjnicall Java_kg_tom_myjni_display (jnienv*env, Jobject obj) {printf ("Hello World tom!!"); return;} Jniexport jdouble jnicall java_kg_tom_myjni_sum (jnienv*env, Jobject obj, jdouble A, jdouble b) {    returnA +b;}

Four:

Compiled into a linked file, which is translated into DLL files under Windows and translated into so files under Linux.

Gcc-wall-d_jni_implementation_-wl,--kill-at-id:/java/include–id:/java/include/win32-shared-o sum.dll java_sum_n Ative.c

Command Simple Introduction
-wall-d_jni_implementation_ Compile our C file into a DLL
-WL,--Kill-at Because MinGW is separated by the @ by default, it causes the JNI mechanism to be unreadable, so it should be deleted.
MinGW Official website explanation
Id:/java/include–id:/java/include/win32 Import the header file that we used for JNI required ... Drive letter, folder to modify itself
-shared-o Output configuration, the first parameter is our output name (random), the second parameter is our C file (random)

Five:

Run JNI, put the compiled DLL into the project root directory, note that it is not the code root directory.

Execution effect:

JNI Small Test Sledgehammer

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.