JNI入门介绍

1.简介
  
  JNI是Java Native Interface的缩写,它的设计目的是:
  
  The standard Java class library may not support the platform-dependent features needed by your application.
  
  You may already have a library or application written in another programming language and you wish to make it accessible to Java applications
  
  You may want to implement a small portion of time-critical code in a lower-level programming language, such as assembly, and then have your Java application call these functions
  
   2.JNI的书写步骤
  
  编写带有native声明的方法的java类
  
  使用javac命令编译所编写的java类
  
  使用javah ?jni java类名生成扩展名为h的头文件
  
  使用C/C++实现本地方法
  
  将C/C++编写的文件生成动态连接库
  
  ok
  
  1) 编写java程序:
  
  这里以HelloWorld为例。
  
  代码1:
  
  class HelloWorld {
  public native void displayHelloWorld();
  
  static {
  System.loadLibrary("hello");
  }
  
  public static void main(String[] args) {
  new HelloWorld().displayHelloWorld();
  }
  }
  
声明native方法:如果你想将一个方法做为一个本地方法的话,那么你就必须声明改方法为native的,并且不能实现。其中方法的参数和返回值在后面讲述。
  
  Load动态库:System.loadLibrary("hello");加载动态库(我们可以这样理解:我们的方法displayHelloWorld()没有实现,但是我们在下面就直接使用了,所以必须在使用之前对它进行初始化)这里一般是以static块进行加载的。同时需要注意的是System.loadLibrary();的参数“hello”是动态库的名字。
  
  main()方法
  
  2) 编译没有什么好说的了
  
  javac HelloWorld.java
  
  3) 生成扩展名为h的头文件
  
  javah ?jni HelloWorld
  
  头文件的内容:
  /* DO NOT EDIT THIS FILE - it is machine generated */
  #include 75137.html

topquan 2006-10-14 16:32 发表评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值