JNI DEMO
make.sh
javac -h . HelloJNI.java g++ -I"$JAVA_HOME/include" -I"$JAVA_HOME/include/dawin" -shared -o libhelloJNI.dylib HelloJNI.cpp javac HelloJNI.java java HelloJNI
HelloJNI.java
public class HelloJNI { private native void helloJNI(String param); static{ System.loadLibrary("helloJNI"); } public static void main(String[] args) { HelloJNI jni = new HelloJNI(); jni.helloJNI("888888"); } }
HelloJNI.h
/* DO NOT EDIT THIS FILE - it is machine generated */ #include <jni.h> /* Header for class HelloJNI */ #ifndef _Included_HelloJNI #define _Included_HelloJNI #ifdef __cplusplus extern "C" { #endif /* * Class: HelloJNI * Method: helloJNI * Signature: (Ljava/lang/String;)V */ JNIEXPORT void JNICALL Java_HelloJNI_helloJNI (JNIEnv *, jobject, jstring); #ifdef __cplusplus } #endif #endif
HelloJNI.cpp
#include "HelloJNI.h" #include <iostream> using namespace std; JNIEXPORT void JNICALL Java_HelloJNI_helloJNI (JNIEnv *env, jobject ,jstring param) { // 1. 获取C风格的字符串 const char *cstr = env->GetStringUTFChars(param, 0); cout <<endl <<"Hello,JNI "<<cstr <<endl ; }