Java native代码编译步骤简书
目的:防止java代码反编译获取密码算法
(1)编写实现类
com.godlet.PasswordAuth.java
(2)编译java代码
javac PasswordAuth.java
//中文乱码问题
javac -encoding utf-8 PasswordAuth.java
(3)切换到代码home目录
cd ..\..\
(4)生成Native 头文件 com_godlet_PasswordAuth.h
javah com.godlet.PasswordAuth
(5)生成动态native动态链接库
//windows 安装MinGW-W64 使用gcc
gcc -c -I"%JAVA_HOME%\include" -I"%JAVA_HOME%\include\win32" com_godlet_PasswordAuth.c -o PasswordAuth.o
gcc -Wl,--add-stdcall-alias -shared -o PasswordAuth.dll PasswordAuth.o
//linux
gcc com_godlet_PasswordAuth.c -I"${JAVA_HOME}/include" -I"${JAVA_HOME}/include/linux" -fpic -shared -o libPasswordAuth.so
(6)运行程序
//运行java程序 java.library.path在项目配置中修改VM options
java -Djava.library.path=. com.godlet.PasswordAuth
//或者在linux /usr/lib目录上放入so库或者创建.so库路径软链接(推荐)
//ln -s /opt/java/libPasswordAuth.so libPasswordAuth.so
//java com.godlet.PasswordAuth