欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

Java调用C(Linux下实现Java本地方法),linuxjava

来源: javaer 分享于  点击 237 次 点评:14

Java调用C(Linux下实现Java本地方法),linuxjava


一、Java类中声明本地方法,使用native关键字

loadLibrary

public static void loadLibrary

(String
 libname)
Loads the system library specified by the libname argument. The manner in which a library name is mapped to the actual system library is system dependent.

The call System.loadLibrary(name) is effectively equivalent to the call

 Runtime.getRuntime().loadLibrary(name) 

 

Parameters:
libname - the name of the library.
Throws:
SecurityException - if a security manager exists and its checkLink method doesn't allow loading of the specified dynamic library
UnsatisfiedLinkError - if the library does not exist.
NullPointerException - if libname is null
See Also:
Runtime.loadLibrary(java.lang.String) , SecurityManager.checkLink(java.lang.String)

二、生成.class文件,并使用javah命令,将.class文件生成.h文件

生成的.h文件如下

三、将刚刚生成的.h文件包含在头文件中,并用C语言实现这些本地方法,代码如下

四、书写makefile文件,编译刚才所写的C代码,生成.so文件,Linux环境下(windows,DLL文件)

其中,“/sandbox/JAVA2S/jdk1.6.0_16”为JDK安装目录

“libMyNative.so”是Java类中

    static {
        System.loadLibrary("MyNative");
    }

静态方法中参数+lib

五、修改.bash_profile文件,配置环境变量,由于生成的.so文件在当前目录下,故

.bash_profile文件设置如下


export LD_LIBRARY_PATH=./

 

六、执行java MyNative命令,结果如下



Linux下实现Java本地方法(Java调用C)完成。

 

参考资料:

http://www.ibm.com/developerworks/cn/java/jnimthds/index.html




 

相关文章

    暂无相关文章
相关栏目:

用户点评