JNI=java native interface, 为java应用程序提供调用本地方法的接口
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
步骤:
1.编写带有native声明的方法的java类
![](/syntaxhighlighting/OutliningIndicators/None.gif)
![](/syntaxhighlighting/OutliningIndicators/None.gif)
![](/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
![](/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
![](/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
![](/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
![](/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
![](/syntaxhighlighting/OutliningIndicators/None.gif)
2,保存后如果没有错误eclipse会编译产生jni/HelloWorld.class
3,假设工程产生的class都在 classes文件夹下(这个文件夹名字跟个人eclipse设置有关,也可能就是工程根目录),那么控制台cd到classes用javah命令产生h 文件,XXX/classes>javah -jni jni.HelloWorld(package不要漏掉),产生的jni_HelloWorld.h文件如下
![](/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
![](/syntaxhighlighting/OutliningIndicators/None.gif)
![](/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
![](/syntaxhighlighting/OutliningIndicators/None.gif)
![](/syntaxhighlighting/OutliningIndicators/None.gif)
![](/syntaxhighlighting/OutliningIndicators/None.gif)
![](/syntaxhighlighting/OutliningIndicators/None.gif)
![](/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
![](/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
![](/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
![](/syntaxhighlighting/OutliningIndicators/None.gif)
![](/syntaxhighlighting/OutliningIndicators/None.gif)
![](/syntaxhighlighting/OutliningIndicators/None.gif)
4,写cpp,实现jni_HelloWorld.h中的函数,这里请注意函数名,cpp里的函数名跟h文件的函数名要一致,如果是从网上 copy的一些例子,函数名跟你例子的名字不一致虽然之后可以正确编译并产生dll,但是最终运行java程序链接的时候会出错,抛出 java.lang.UnsatisfiedLinkError异常,本人就在此浪费了很长时间。
jni_HelloWorldImp.cpp内容如下:
![](/syntaxhighlighting/OutliningIndicators/None.gif)
![](/syntaxhighlighting/OutliningIndicators/None.gif)
![](/syntaxhighlighting/OutliningIndicators/None.gif)
![](/syntaxhighlighting/OutliningIndicators/None.gif)
![](/syntaxhighlighting/OutliningIndicators/None.gif)
![](/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
![](/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
5,编译cpp产生dll,在控制台下>cl -I%java_home%\include -I%java_home%\include\win32 -LD jni_HelloWorldImp.cpp -Fehellodll.dll
以上命令的参数中,-I表示编译包含的额外目录,-LD表示产生dll,-Fe后面表示产生dll的名字,具体的参数可以自己用cl -help命令查看,cl环境配置可以参考
用vs编译器cl在控制台下编译一文
如果成功编译,会产生hellodll.dll,hellodll.lib,hellodll.exp三个文件,将hellodll.dll考到工程根目录下。
6,运行java程序,输出Hello world !
转载于:https://blog.51cto.com/xiaomao870311/545717