java 调用dll文件总结

作者: 2hei 发表于2008年4月17日 22:37
版权声明: 可以转载, 转载时务必以超链形式标明文章原始出处和作者信息及版权声明
http://www.2hei.net/mt/2008/04/java-dll-use.html

java 调用dll文件时

几个注意点:
1. package的使用
2. javah的使用
3.path路径的设定

 

下面实例介绍java调用dll中的Max函数:

hello.java

package 2hei.net.dll;

public class hello
{
    static
    {
        //System.out.println(System.getProperties().get("java.library.path"));
        System.loadLibrary("Hello");
    } 
    public native static int Max(int a,int b);
   
    public static void main(String[] args)
    {
        int maxnum = 0;
        int aa = 10;
        int bb = 11;
        hello hi= new hello();
        maxnum = hi.Max(aa,bb);
        System.out.println("max is "+maxnum);        
    }
}

生成.h头文件

createh.bat

cd E:\src\java\2hei\net\dll  

javah hello

会生成一个2hei_net_dll_hello.h的文件

编辑编辑 2hei_net_dll_hello.h  把#include <jni.h> 改成#include "jni.h"

从jdk的目录里面找到jni.h  和 jni_md.h

下面使用VC++生成dll文件。

新建一个dll工程,比如Hello  编辑Hello.cpp

// Hello.cpp : Defines the entry point for the DLL application.
//

#include "stdafx.h"
#include "Hello.h"
#include "2hei_net_dll_hello.h"

JNIEXPORT jint JNICALL 2hei_net_dll_hello_Max
  (JNIEnv *, jclass, jint a, jint b)
{
 if(a>=b)return a;
 else
 return b;
}

编译工程后,在Debug目录中找到Hello.dll文件,放到java的path目录下面。

执行hello.java 即可以得到想要的结果。

 

| | Comments (0) | TrackBacks (0)
本篇相关文章:

构造函数和析构函数 - 2008-04-08

C++中的虚函数(学习) - 2008-04-02

引用 C++ - 2008-03-27

在C++中使用delete、free - 2008-03-06

发表一个评论

关于这篇文章

这篇文章由2hei2008年4月17日 22:37发布.

上一篇:使用vbs脚本写注册表来设置Ghostscript环境

下一篇:date_default_timezone_set

回到首页 或者查看归档文章

  • Powered by FeedBurner
  • Add to Google Reader or Homepage
  • Add to My AOL
  • Subscribe in NewsGator Online
  • del.icio.us/2heidel.icio.us/2hei
  • Subscribe to feed feeds