四、书写makefile文件,编译刚才所写的C代码,生成.so文件,Linux环境下(windows,DLL文件)
libMyNative.so:MyNative.o makefile
gcc -Wall -rdynamic -shared -o libMyNative.so MyNative.o
MyNative.o:MyNative.c MyNative.h
gcc -Wall -c MyNative.c -I./ -I/sandbox/Java2S/jdk1.6.0_16/include -I/sandbox/JAVA2S/jdk1.6.0_16/include/linux
cl:
rm -rf *.o *.so
libMyNative.so:MyNative.o makefile
gcc -Wall -rdynamic -shared -o libMyNative.so MyNative.o
MyNative.o:MyNative.c MyNative.h
gcc -Wall -c MyNative.c -I./ -I/sandbox/JAVA2S/jdk1.6.0_16/include -I/sandbox/JAVA2S/jdk1.6.0_16/include/linux
cl:
rm -rf *.o *.so
其中,“/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)完成。