更多精彩内容,请见:http://www.16boke.com
by zxy,QQ群:168424095
(1)编写java代码JniDemo.java
class JniDemo
{
public native void printHello();
public native int add(int a, int b);
static
{
System.loadLibrary("JniDllDemo");
}
public static void main(String[] args)
{
JniDemo demo = new JniDemo();
demo.printHello();
int a = 3;
int b = 4;
System.out.println(a + "和" + b + "的和是:" + demo.add(3, 4));
}
}
(2)用javac JniDemo.java命令编译,生成JniDemo.class文件
(3)用javah JniDemo命令生成扩展名为.h的头文件
JNIEXPORT void JNICALL Java_JniDemo_printHello(JNIEnv *, jobject)
{
cout<<"Hello!"<<endl;
}
JNIEXPORT jint JNICALL Java_JniDemo_add(JNIEnv *, jobject, jint a, jint b)
{
return a+b;
}
生成Release版本JniDllDemo.dll
(5)将JniDllDemo.dll和JniDemo.class放在同一目录下,用java JniDemo命令运行可以看到输出:
Hello!
3和4的和是:7
源码下载:JniDemo
更多精彩内容,请见:http://www.16boke.com