JAVA JNI Principles
JNI is an important function of the Java standard platform, it makes up for the Java platform-independent of the significant advantages of the lack of Java implementation across the platform, but also with other languages (such as C, C + +) Dynamic library interaction, to give other languages the opportunity to play an advantage.
With the support of the Java standard platform, the JNI schema is easier to implement and use. Here is a summary of the following knowledge map:
Instance:
Environment Description: Ubuntu 10.4.2 LTS System
Program List 1:src/com/magc/jni/helloworld.java
/**
* * * * * *
package com.magc.jni;
/**
* @author MAGC
*
*
/public class HelloWorld {
static {
system.loadlibrary ("Hello");
}
public native void Displayhello ();
/**
* @param args */public
static void Main (string[] args) {
new HelloWorld (). Displayhello ();
}
Into the SRC directory, compile the Java class,
Command:javac./com/magc/jni/helloworld.java
Generate Helloworld.class in the directory where the Helloworld.java resides
Then use Javah to generate the header file,
Command:javah-jni Com.magc.jni.HelloWorld
Generates a Com_magc_jni_helloworld.h header file in the current directory for C, C + + programs to reference and implement functions
Program List 2:com_magc_jni_helloworld.h
/* Do isn't EDIT this file-it is machine generated/*
#include <jni.h>/
* Header for Class Com_magc_jni_hell Oworld * *
#ifndef _included_com_magc_jni_helloworld
#define _included_com_magc_jni_helloworld
#ifdef _ _cplusplus
extern "C" {
#endif/*
Class: com_magc_jni_helloworld
* Method: Displayhello
* Signature: () V
/
jniexport void Jnicall Java_com_magc_jni_helloworld_displayhello
(jnienv *, jobject);
#ifdef __cplusplus
}
#endif
#endif
Note: 1, this header file is not required to compile the user, directly for other C, C + + program references.
2, the Java_com_magc_jni_helloworld_displayhello (jnienv *, Jobject) method in this header file is the interface that will interact with the dynamic link library in the future, and needs to be consistent with the name.
Program List 3:src/jni_helloworldimpl.cpp
#include <jni.h>
#include "com_magc_jni_helloworld.h"
#include <stdio.h>
jniexport void Jnicall Java_com_magc_jni_helloworld_displayhello
(jnienv *env, jobject obj)
{
printf ("from Jni_ HelloworldImpl.cpp: ");
printf ("Hello world!") \ n ");
return;
}
This C + + file implements the functions in the header file above, noting that the method function name should be consistent.
Compilation generates dynamic library libhello.so,
Command:g++-shared-i/usr/lib/jvm/java-6-openjdk/include jni_helloworldimpl.cpp-o libhello.so
Once successful, the dynamic link library libhello.so file is generated in the current directory.
With the implementation of the dynamic library, you can run Java invoke the JNI program class native method,
Command:java-djava.library.path=. Com.magc.jni.HelloWorld
The input result is: fromJni_helloworldImpl.cpp:Hello world!
Thank you for reading, I hope to help you, thank you for your support for this site!