package cn.undoner.utils; /** * Created by ${undoner<a data-cke-saved-href="mailto:[email protected]" href="mailto:[email protected]"></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/* 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; }</stdio.h></jni.h>
/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