JNI-Java Native Interface

Java提供了JNI机制,用来弥补与平台无关的特性,可以使用C/C++编写本地代码生成动态连接库交由Java调用。

首先创建一个NativeHelloWorld.java文件,包含以下内容;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
//java本地方法要由native声明
public class NativeHelloWorld{
//使用静态加载块加载dll
static{
System.loadLibrary( " NativeHelloWorld " );
}
//native声明的start方法,交由c/c++实现,因此无需包含方法体;
native void start(String s);
public static void main(String[] args)
{
//实例化对象并调用start方法;
new NativeHelloWorld().start( " I am loaded by Native code of c++ " );
}
}

Read More