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(); } }
/* 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
#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; }
/usr/lib/jvm/java/jre/lib/amd64/server/MyJni.so
8.
执行CplusUtil.class
在ide中运行CplusUtil或者直接java命令执行:
java cn.undoner.utils.CplusUtil
9.
结果:
/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