/**
 * Created on 2006年11月28日, 下午16.52
 * @author  郑小微
 */
testdll.java
public class testdll
{
static
{
System.loadLibrary("xiaoweidll");
}
public native static int get();
public native static void set(int i);
public static void main(String[] args)
{
testdll test = new testdll();
test.set(10);
System.out.println(test.get());
}
}
编译JAVA源文件
javac testdll.java
生成testdll.class

编译java头文件
javah -jni testdll
生成testdll.h

新建vc++项目的动态连接库

将头文件testdll.h包含到vc项目中,且新建一文件xxx.cpp来实现testdll.h中的方法
即可。最后编译,运行。在项目下的DEBUG中可以找到生成的.dll文件,文件名和项目名字相同。最后将生成的.dll文件拷贝到你java的项目中。
切记:不要将System.loadLibrary("xiaoweidll");
写为System.loadLibrary("xiaoweidll.dll");
可能遇见下列错误:
Cannot open include file: 'jni.h': No such file or directory
Error executing cl.exe.
进行以下步骤即可解决:
vc的tools/options/directory中加入jni.h和jni_md.h两个文件所在的绝对路径,在/jdk/include和/jdk/include/win32下。

附录:1.testdll.h
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class testdll */

#ifndef _Included_testdll
#define _Included_testdll
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     testdll
 * Method:    get
 * Signature: ()I
 */
JNIEXPORT jint JNICALL Java_testdll_get
  (JNIEnv *, jclass);

/*
 * Class:     testdll
 * Method:    set
 * Signature: (I)V
 */
JNIEXPORT void JNICALL Java_testdll_set
  (JNIEnv *, jclass, jint);

#ifdef __cplusplus
}
#endif
#endif

2. testdll.cpp
#include "testdll.h"
int i = 0;
JNIEXPORT jint JNICALL Java_testdll_get (JNIEnv *, jclass)
{
return i;
}
JNIEXPORT void JNICALL Java_testdll_set (JNIEnv *, jclass, jint j)
{
i = j;
}