JNI:Java Native Interface,是Java语言提供的一种通用接口,用于Java代码与本地化代码的交互。所谓本地化代码是指直接编译成的与机器相关的二进制代码,而非Java字节码之类的中间代码。Windows下面的可执行文件,DLL等,Linux下面的可执行文件和SO文件等,都是二进制代码。
下面通过一个例子来说明在JAVA中调用C++的基本步骤,相信通过这个例子你能明白Java和C/C++之间的参数传递。
(1).编写带有native的java类
用eclipse创建工程testJNI,编写2个类MyJNI和TestStructures,敲入以下代码,然后编译。
package test;
public class MyJNI {
public native int intMethod(int n); //返回一个int数据
public native boolean booleanMethod(boolean bool); //返回一个Boolean
public native String stringMethod(String text); //返回一个字符串
public native int intArrayMethod(int[] intArray); //返回传入int数组的和
private native String[] getStringArray(String[] sa); //返回字符串数组
public static native TestStructures structMethod(TestStructures data); // 返回一个结构体
static {
// System.loadLibrary("testJNI"); //加载动态链接库
System.load("E:\\Workspaces\\CodeBlocks\\testJNI\\bin\\Debug\\testJNI.dll");
}
public static void main(String[] args) {
MyJNI sample = new MyJNI();
int square = sample.intMethod(5);
bool