Thinking in java:  JNI

解决方案 »

  1.   

    System.loadLibrary("Library DLL Name");public native void method();
      

  2.   

    http://www.csdn.net/Expert/TopicView1.asp?id=879129
      

  3.   

    其实java调用dll是和c分不开的,下面给出一个java调用windows中dll的函数的例子。程序如下hellodll.java
    public class hellodll
    {
           static
           {
                  System.loadLibrary("sunpengjie");
           }
           public native static String helloget();
           public native static void helloset(int helloword);  
           public static void main(String[] args)
           {
                  hellodll hello = new hellodll();
                  hello.helloset(1);
                  System.out.println(hello.helloget());
           }
    }
    用javac hellodll.java编译它,会生成hellodll.class
    再用javah hellodll,则会在当前目录下生成hellodll.hhellodll.cpp文件#include "hellodll.h"
    String str = "";
    //在产生的hellodll.h中会看到两个函数的原型
    JNIEXPORT ? JNICALL Java_hellodll_helloget (JNIEnv *, jclass)
    {
           return str;
    }
    JNIEXPORT void JNICALL Java_hellodll_helloset (JNIEnv *, jclass, jint j)
    {
           if (j>0)
           { str = "This is a helloword Program!!!!!!" ;}
           if(j<=0)
           { str = "This is not a helloword Program!!!!!!";}
    }编译连接成库文件,在WINDOWS下生成sunpengjie.dll。把sunpengjie.dll拷贝到hellodll.class的目录下,java hellodll就可以运行了。