WinMsgBox.java
package com.jni.test;
public class WinMsgBox {
static{
System.loadLibrary("WinMsgDll");
// System.load("C:\\WINDOWS\\system32\\WinMsgDll.dll");
}
public native void showMsgBox(String str);
}
编译
javac -d E:\test\demo\src\com\jni\test WinMsgBox .java
将会在 E:\test\demo\src\com\jni\test 目录下生产一个目录树 目录树下生成 WinMsgBox .class
生成 com_jni_test_WinMsgBox.h
javah -jni com.jni.test.WinMsgBox
com_jni_test_WinMsgBox.h
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_jni_test_WinMsgBox */
#ifndef _Included_com_jni_test_WinMsgBox
#define _Included_com_jni_test_WinMsgBox
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_jni_test_WinMsgBox
* Method: showMsgBox
* Signature: (Ljava/lang/String;)Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_com_jni_test_WinMsgBox_showMsgBox
(JNIEnv *, jobject, jstring);
#ifdef __cplusplus
}
#endif
#endif
使用VC来编写本地方法的实现函数,最后编译成.dll文档。过程如下:
1) 选择new->projects(选择Win32 Dynamic-Link Library,以Step 1中指定的库名WinMsgDll作为工程名)->OK->An ampty DLL project->Finish。
2) 选择Tools->Options->Directories(添加目录D:\J2SDK1.4.2_03\INCLUDE和D:\J2SDK1.4.2_03\INCLUDE\WIN32)。在这些目录中包含JNI所需的头文档。
3) 将Step 3生成的edu_netcom_jni_WinMsgBox.h拷贝到WinMsgDll工程文档夹中。然后FileView中添加这个头文档。
4) 添加源文档WinMsgDll.cpp,内容如下:
#include "windows.h"
#include "com_jni_test_WinMsgBox.h"
#include <stdio.h>
/*
* Class: edu_netcom_jni_WinMsgBox
* Method: showMsgBox
* Signature: (Ljava/lang/String;)V
*/
JNIEXPORT void JNICALL Java_com_jni_test_WinMsgBox_showMsgBox
(JNIEnv * env, jobject obj, jstring str){
const char *msg;
msg = env->GetStringUTFChars(str,0);
MessageBox(NULL,msg,"Java invoke",MB_OK);
env->ReleaseStringUTFChars(str,msg);
return ;
}
编译生成WinMsgBox.dll文档拷贝到 jdk的bin下活system32下
java测试
package com.jni.test;
public class TestWinMsgBox {
public static void main(String []args){
WinMsgBox box=new WinMsgBox();
System.out.println("java.library.path :"+System.getProperty("java.library.path"));
box.showMsgBox("the first test!");
}
}
package com.jni.test;
public class WinMsgBox {
static{
System.loadLibrary("WinMsgDll");
// System.load("C:\\WINDOWS\\system32\\WinMsgDll.dll");
}
public native void showMsgBox(String str);
}
编译
javac -d E:\test\demo\src\com\jni\test WinMsgBox .java
将会在 E:\test\demo\src\com\jni\test 目录下生产一个目录树 目录树下生成 WinMsgBox .class
生成 com_jni_test_WinMsgBox.h
javah -jni com.jni.test.WinMsgBox
com_jni_test_WinMsgBox.h
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_jni_test_WinMsgBox */
#ifndef _Included_com_jni_test_WinMsgBox
#define _Included_com_jni_test_WinMsgBox
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_jni_test_WinMsgBox
* Method: showMsgBox
* Signature: (Ljava/lang/String;)Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_com_jni_test_WinMsgBox_showMsgBox
(JNIEnv *, jobject, jstring);
#ifdef __cplusplus
}
#endif
#endif
使用VC来编写本地方法的实现函数,最后编译成.dll文档。过程如下:
1) 选择new->projects(选择Win32 Dynamic-Link Library,以Step 1中指定的库名WinMsgDll作为工程名)->OK->An ampty DLL project->Finish。
2) 选择Tools->Options->Directories(添加目录D:\J2SDK1.4.2_03\INCLUDE和D:\J2SDK1.4.2_03\INCLUDE\WIN32)。在这些目录中包含JNI所需的头文档。
3) 将Step 3生成的edu_netcom_jni_WinMsgBox.h拷贝到WinMsgDll工程文档夹中。然后FileView中添加这个头文档。
4) 添加源文档WinMsgDll.cpp,内容如下:
#include "windows.h"
#include "com_jni_test_WinMsgBox.h"
#include <stdio.h>
/*
* Class: edu_netcom_jni_WinMsgBox
* Method: showMsgBox
* Signature: (Ljava/lang/String;)V
*/
JNIEXPORT void JNICALL Java_com_jni_test_WinMsgBox_showMsgBox
(JNIEnv * env, jobject obj, jstring str){
const char *msg;
msg = env->GetStringUTFChars(str,0);
MessageBox(NULL,msg,"Java invoke",MB_OK);
env->ReleaseStringUTFChars(str,msg);
return ;
}
编译生成WinMsgBox.dll文档拷贝到 jdk的bin下活system32下
java测试
package com.jni.test;
public class TestWinMsgBox {
public static void main(String []args){
WinMsgBox box=new WinMsgBox();
System.out.println("java.library.path :"+System.getProperty("java.library.path"));
box.showMsgBox("the first test!");
}
}