服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

设计java程序与c语言的接口


  第一步:声明本地变量

  例如:

class nativehello{
 public native void nativehelloworld();
 static{
  system.loodlibrary("nativetest");//调用nativetest.dll库文件
 }

  第二步:生成头文件

  先用javac编译nativehello.java,再用javah生成c的头文件.h文件

  第三步:生成根文件

  命令如下:javah -stubs nativehello (生成nativehello.c文件)

  第四步:编写c程序(此处假定文件名为nativetest.c)

#include <stdio.h>
#include <nativehello.h>//指第二步生成的.h文件
#include <stubpreamble.h>//指jdk的include下的文件
void nativehello_nativehelloworld(struct hnativehello *this){
.........
}
/*函数名nativehello_nativehelloworld不能任意指定,可以从javah生成的头文件中查到,也可用

  如下方法命名:类名_本地方法名(struct h类名 *this)*/

  第五步:编译dll文件

  将nativetest.c和nativehello.c编译成dll库文件,文件名与system.loodlibrary("nativetest")中的文件同名。

  最后讲一下测试的方法,源文件如下:

class usenative{
 public static void main(string []args){
  nativehello nh=new nativehello();
  nh.nativehelloworld();
 }
}

扫描关注微信公众号