1.
CplusUtil.java is a tool class in the java web project. The
content is as follows:
CplusUtil.java
2. The
entire project can be compiled through IDE tools (Eclipse/IntellJ/javac, etc.) to generate corresponding class files. The path of
this article is:
/project name/target/classes/cn/undoner/utils/CplusUtil.class
3. Required for utils The called c function generates the corresponding .h header file.
Note : In the case of a package name, remember to bring the package name path
javah -classpath classes -jni cn.undoner.utils.CplusUtil to generate the .h header file.
Generate the file: cn_undoner_utils_CplusUtil.h
The content is as follows:
4. Write c function
File : MyJni.c
content is as follows:
5.
Compile the .c file into .o, and then re-convert it into a .so or .dll file
gcc -fPIC -D_REENTRANT -I /usr/lib/jvm/java/include -I /usr/lib/jvm/java/include /linux -c MyJni.c
Generated file: MyJni.o
6.
Compile the .o file into a .so file
gcc -shared MyJni.o -o MyJni.so
Generated file: MyJni.so
7.
The path of MyJni.so in this article is:
CplusUtil.java is a tool class in the java web project. The
content is as follows:
CplusUtil.java
package cn.undoner.utils;
/**
* Created by ${<A HREF="mailto:[email protected]">undoner</A>} on 16-2-25.
*/
public class CplusUtil {
public native void sayHello();
static{
System.out.println(System.getProperty("java.library.path"));
System.load("/usr/lib/jvm/java/jre/lib/amd64/server/MyJni.so");
}
public static void main(String[] args){
CplusUtil h = new CplusUtil();
h.sayHello();
}
}
2. The
entire project can be compiled through IDE tools (Eclipse/IntellJ/javac, etc.) to generate corresponding class files. The path of
this article is:
/project name/target/classes/cn/undoner/utils/CplusUtil.class
3. Required for utils The called c function generates the corresponding .h header file.
Note : In the case of a package name, remember to bring the package name path
javah -classpath classes -jni cn.undoner.utils.CplusUtil to generate the .h header file.
Generate the file: cn_undoner_utils_CplusUtil.h
The content is as follows:
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class cn_undoner_utils_CplusUtil */
#ifndef _Included_cn_undoner_utils_CplusUtil
#define _Included_cn_undoner_utils_CplusUtil
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: cn_undoner_utils_CplusUtil
* Method: sayHello
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_cn_undoner_utils_CplusUtil_sayHello
(JNIEnv *, jobject);
#ifdef __cplusplus
}
#endif
#endif
4. Write c function
File : MyJni.c
content is as follows:
#include <jni.h>
#include "cn_undoner_utils_CplusUtil.h"
#include <stdio.h>
JNIEXPORT void JNICALL Java_cn_undoner_utils_CplusUtil_sayHello(JNIEnv *env,jobject obj){
printf("Hello JNI");
return;
}
5.
Compile the .c file into .o, and then re-convert it into a .so or .dll file
gcc -fPIC -D_REENTRANT -I /usr/lib/jvm/java/include -I /usr/lib/jvm/java/include /linux -c MyJni.c
Generated file: MyJni.o
6.
Compile the .o file into a .so file
gcc -shared MyJni.o -o MyJni.so
Generated file: MyJni.so
7.
The path of MyJni.so in this article is:
/usr/lib/jvm/java/jre/lib/amd64/server/MyJni.so
8.
Execute CplusUtil.class
to run CplusUtil in ide or execute the java command directly:
java cn.undoner.utils.CplusUtil
9.
Result:
/usr/lib/jvm/java/jre/lib/amd64/server:/usr/lib/jvm/java/jre/lib/amd64:/usr/lib/jvm/java/jre/../ lib/amd64:/home/vobile/java_tool/idea-IU-141.178.9/bin::/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
Hello JNI
Note: System.getProperty("java.library.path") outputs the following information as the local lib path:
/usr/lib/jvm/java/jre/lib/amd64/server:/usr/lib/jvm/java/ jre/lib/amd64:/usr/lib/jvm/java/jre/../lib/amd64:/home/vobile/java_tool/idea-IU-141.178.9/bin::/usr/java/packages/lib /amd64:/usr/lib64:/lib64:/lib:/usr/lib