1. Write a Java program that contains a native function.
package cn.vicky.jni;/** * * @author Vicky.H */public class HelloWorld { // 1.need a native method public native void sayHello(); }
2. Compile the Java program.
[Root @ localhost SRC] # tree
.
'-- CN
'-- Vicky
'-- JNI
'-- Helloworld. Java
3 directories, 1 file
[Root @ localhost SRC] # javac cN/Vicky/JNI/helloworld. Java
[Root @ localhost SRC] # tree
.
'-- CN
'-- Vicky
'-- JNI
| -- Helloworld. Class
'-- Helloworld. Java
3 directories, 2 files
[Root @ localhost SRC] # javah CN. Vicky. JNI. helloworld
[Root @ localhost SRC] # tree
.
| -- CN
| '-- Vicky
| '-- JNI
| -- Helloworld. Class
| '-- Helloworld. Java
'-- Cn_vicky_jni_helloworld.h
3 directories, 3 files
If the preceding operations are performed, a corresponding C/C ++ header file is generated.
3. Write a C/C ++ dynamic file library.
The dynamic file library in. So format is still written using netbeans + centos. The project directory is as follows:
Cn_vicky_jni_helloworld.h is the header file generated by the javah command. JNI. h and jni_md.h are:
[Root @ localhost SRC] # cd/usr/Java/default/include/
[Root @ localhost include] # tree
.
| -- Classfile_constants.h
| -- Jawt. h
| -- Jdwptransport. h
| -- JNI. h
| -- Jvmti. h
'-- Linux
| -- Jawt_md.h
'-- Jni_md.h
1 directory, 7 files
Copy the corresponding file.
Compile the header file: cn_vicky_jni_helloworld_impl.cpp
#include "cn_vicky_jni_HelloWorld.h"#include <iostream>using namespace std;JNIEXPORT void JNICALL Java_cn_vicky_jni_HelloWorld_sayHello (JNIEnv *env, jobject obj){ cout << "hello world" << endl;}
Is a simple program that outputs "Hello World. Generate a program: Obtain the dynamic link library of libjni_01_cpp.so. To only use this dynamic link library, we need to copy it to the/usr/lib file.
4. Java calls libjni_01_cpp.so
Continue to write the helloworld. Java program
package cn.vicky.jni;/** * * @author Vicky.H */public class HelloWorld { // 1.need a native method public native void sayHello(); public static void main(String[] args) { System.loadLibrary("jni_01_cpp"); new HelloWorld().sayHello(); }}
[Vicky @ localhost SRC] $ javac cN/Vicky/JNI/helloworld. Java
[Vicky @ localhost SRC] $ tree
.
| -- CN
| '-- Vicky
| '-- JNI
| -- Helloworld. Class
| '-- Helloworld. Java
'-- Cn_vicky_jni_helloworld.h
3 directories, 3 files
[Vicky @ localhost SRC] $ Java CN. Vicky. JNI. helloworld
Hello World
Program Execution, complete. Delete the dynamic link library used for testing:
[Root @ localhost SRC] # rm/usr/lib/libjni_01_cpp.so
RM: Do you want to delete the common file "/usr/lib/libjni_01_cpp.so "? Y
Certificate ------------------------------------------------------------------------------------------------------------------------------------------------------------------------
The above is used by JNI in Linux. For Windows, it is basically the same. When the jni_01_cpp.dll file is generated, we need to address the directory where jni_01_cpp.dll is located, baby to the path in windows. For example:
Then run the helloworld program.