linux下调用C/C++生成的so文件
Java在linux下调用C/C++生成的so文件
CplusUtil.java是java web工程中的一个工具类
内容如下:
CplusUtil.java
[java] view plaincopyprint?
-
package cn.undoner.utils;
-
/**
- Created by ${<A HREF="mailto:undoner@gmail.com">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();
-
}
-
}
可通过IDE工具(Eclipse/IntellJ/javac等)编译整个工程,生成相应class文件
本文路经为:
/工程名/target/classes/cn/undoner/utils/CplusUtil.class
3.为utils所需调用的c函数生成相应.h头文件
注意:在有包名情况下,生成.h头文件要记得带上包名路径
javah -classpath classes -jni cn.undoner.utils.CplusUtil
生成文件:cn_undoner_utils_CplusUtil.h
内容如下:
[cpp] view plaincopyprint?
-
/* 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.编写c函数
文件:MyJni.c
内容如下:
[cpp] view plaincopyprint?
-
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;
-
}
将.c文件编译成.o,再重新转换成.so或者.dll文件
gcc -fPIC -D_REENTRANT -I /usr/lib/jvm/java/include -I /usr/lib/jvm/java/include/linux -c MyJni.c
生成文件:MyJni.o
将.o文件编译成.so文件
gcc -shared MyJni.o -o MyJni.so
生成文件:MyJni.so
本文MyJni.so路径为:
/usr/lib/jvm/java/jre/lib/amd64/server/MyJni.so
执行CplusUtil.class
在ide中运行CplusUtil或者直接java命令执行:
java cn.undoner.utils.CplusUtil
结果:
/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
注:System.getProperty("java.library.path")输出以下信息为本机的lib路径:
/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