JNI 小解
唉,最近总是搞些开源的东东,接触的知识十分的乱,今天玩c/s的东东,明天搞服务器,也没有专注的去搞些东西。看看,今天又来了JNI,去网上逛了一圈,搞了些战利品。比葫芦画瓢,自己写了一个经典的 hello world!,简简单单的入门小例子,还请高手们多多指教。代码如下: /**
-
@author Andy
-
2011-7-20
-
/ public class Sample1 { /*
- 声明4个本地的方法,用于调用C或c++中的方法
- **/ public native int intMethod(int n); public native boolean booleanMethod(boolean bool); public native String stringMethod(String text); public native int intArrayMethod(int intArray);
public static void main(String args) { System.loadLibrary("Sample1");//加载Sample1桥接文件 Sample1 sample = new Sample1();
int square = sample.intMethod(5); boolean bool = sample.booleanMethod(true); String text = sample.stringMethod("JAVA"); int sum = sample.intArrayMethod( new int{1,1,2,3,5,8,13} );
System.out.println("intMethod: " + square); System.out.println("booleanMethod: " + bool); System.out.println("stringMethod: " + text); System.out.println("intArrayMethod: " + sum); } }
用jdk中的javah,生成Sample1.h文件 -------------------未完-------------------