http://blog.chinaunix.net/uid-10275706-id-3241480.html
1.新建文件夹test.
2.在test文件夹中放置文件HelloWorld.java:
package test;
public class HelloWorld {
public static void main(String[] args) {
System.loadLibrary("HelloWorld");
printHello();
}
public static native final void printHello();
}
3.编译HelloWorld.java:
javac test/HelloWorld.java
4.在test文件夹中放置文件HelloWorld.cpp:
#include <iostream>
#include <jni.h>
using namespace std;
static int android_print(JNIEnv * env, jclass clazz) {
cout<<"helloworld"<<endl;
}
static JNINativeMethod gMethods[] = {
{"printHello", "()V", (void*)android_print},
};
jint JNI_OnLoad(JavaVM* vm, void* reserved) {
JNIEnv* env = NULL;
if (vm->GetEnv((void**) &env, JNI_VERSION_1_6) != JNI_OK)
return -1;
jclass clazz = (env)->FindClass("test/HelloWorld");
if((env)->RegisterNatives(clazz, gMethods, 1)< 0)
return -1;
return JNI_VERSION_1_6;
}
5.编译HelloWorld.cpp:
g++ test/HelloWorld.cpp -fPIC -shared -o test/libHelloWorld.so -I /usr/lib/jvm/java-7-openjdk-i386/include/
6.设置环境变量:
export LD_LIBRARY_PATH=./test
7.目录结构:
./test:
HelloWorld.class HelloWorld.cpp HelloWorld.java libHelloWorld.so
8.运行:
java test.HelloWorld
helloworld