当使用到JNI的时候,基本可以肯定Java的平台移植性注定减弱,接下来记录一次使用Java JNI开发的经历。
? ? 下面是使用JNI常见三种场景:
? ? 1.在Java应用中标准Java类库不支持平台相关的特性
? ? 2.已经存在用其它语言写好的类库,希望通过Java JNI类访问
? ? 3.需要使用低级语言(如汇编)来实现时效性要求很高的一小部分代码
? ? 这次使用JNI属于第2中场景,由于加解密库使用C来实现的,而在Java应用中使用到其加密后的密文数据,所以解密部分需要此库。
? ? 在1和3这两种场景下使用JNI做法相对容易一些,通常先定义好本地方法,然后通过javah生成头文件,接着用其它语言(如C)来实现相应的功能,而2中场景则需要做一些简单的适配,因为类库已经存在,而在Java中定义好的本地方法并不能直接对应类库的具体实现,所以得通过调用已存在的类库中的方法来实现本地方法。
在开始之前有一个坑先看看:
? ?
? ? 本地编译好的动态库头信息:
[ enc]$ readelf -a libfdsi.so?
ELF Header:
? Magic:? 7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00?
? Class:? ? ? ? ? ? ? ? ? ? ? ? ? ? ELF64
? Data:? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 2's complement, little endian *******
? Version:? ? ? ? ? ? ? ? ? ? ? ? ? 1 (current)
? OS/ABI:? ? ? ? ? ? ? ? ? ? ? ? ? ? UNIX - System V
? ABI Version:? ? ? ? ? ? ? ? ? ? ? 0
? Type:? ? ? ? ? ? ? ? ? ? ? ? ? ? ? DYN (Shared object file)
? Machine:? ? ? ? ? ? ? ? ? ? ? ? ? Advanced Micro Devices X86-64
? ?
? ? 提供方静态库信息:
?
ELF Header:
? Magic:? 7f 45 4c 46 02 02 01 00 00 00 00 00 00 00 00 00?
? Class:? ? ? ? ? ? ? ? ? ? ? ? ? ? ELF64
? Data:? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 2's complement, big endian
? Version:? ? ? ? ? ? ? ? ? ? ? ? ? 1 (current)
? OS/ABI:? ? ? ? ? ? ? ? ? ? ? ? ? ? UNIX - System V
? ABI Version:? ? ? ? ? ? ? ? ? ? ? 0
? Type:? ? ? ? ? ? ? ? ? ? ? ? ? ? ? REL (Relocatable file)
? Machine:? ? ? ? ? ? ? ? ? ? ? ? ? PowerPC64
通过对比应该很清楚了,数据存储模式不同。这里需要明确的是环境一致性很重要。
接下来来从头到尾实现一个Java调用C的一个解密方法。
1.定义Java的本地方法(DataDecryt.java)
? ?
package com.cto;
?
public class DataDecrypt{
?
? ? native public static String decrypt(String data);
?
}
? 2.通过javah命令生成头文件(dd.h)
./javah -classpath . -jni -o dd.h com.cto.DataDecrypt
?
/* DO NOT EDIT THIS FILE - it is machine generated */
#include
/* Header for class com_cto_DataDecrypt */
?
#ifndef _Included_com_cto_DataDecrypt
#define _Included_com_cto_DataDecrypt
#ifdef __cplusplus
extern "C" {
#endif
/*
?* Class:? ? com_cto_DataDecrypt
?* Method:? ? decrypt
?* Signature: (Ljava/lang/String;)Ljava/lang/String;
?*/
JNIEXPORT jstring JNICALL Java_com_cto_DataDecrypt_decrypt
? (JNIEnv *, jclass, jstring);
?
#ifdef __cplusplus
}
#endif
#endif
3.定义使用静态库中的方法的头文件(dec.h)
#ifndef __DEC__
#define _DEC__
?
#ifdef __cplusplus
extern "C"{
#endif
?
? int ts_comm_dec(const char* in , int inlen, char* out, int* outlen);
?
#ifdef __cplusplus
}
#endif
#endif
? ? ts_comm_dec方法即为已经实现了的解密方法。
?4.创建实现dd.h头文件方法的cto.c文件,cto.c中将调用ts_common_dec方法
?
#include
#include
#include "dec.h"
#include "dd.h"
?
//about JNI http://doc.oracle.com/javase/1.5.0/docs/guide/jni/spec/functions.html
JNIEXPORT jstring JNICALL Java_com_cto_DataDecrypt_decrypt
? (JNIEnv *env, jclass jc, jstring data){
?
? char out_str[48];
?
? const char *enc_str = (*env)->GetStringUTFChars(env, data, 0);
? const jsize? enc_len = (*env)->GetStringUTFLength(env, data);
?
? int? out_len = sizeof(out_str);
?
? ts_comm_dec(enc_str, enc_len, out_str, &out_len);
?
? jstring plain_text = (*env)->NewStringUTF(env, out_str);
?
? (*env)->ReleaseStringUTFChars(env, data, enc_str);
?
? return plain_text;
}
?5.编写测试用例(TestDataDecrypt.java)
? 这里加载的类库cto即为libcto.so。关于动态库静态库命名规则可百度之。
package com.cto;
import com.cto.DataDecrypt;
?
public class TestDataDecrypt{
?
? ? static {
? ? ? ? System.loadLibrary("cto");
? ? }
?
? ? public static void main(String [] args){
? ? ? ? String plainText= DataDecrypt.decrypt(args[0]);
?
? ? ? ? System.out.println(plainText);
? ? ? ? System.out.println("解密之后的长度是:"+plainText.length());
? ? }
}
?6.编译动态库
?
gcc cto.c -shared -fPIC -lstdc++? -I~/soft/jdk1.6.0_45/include -I~/soft/jdk1.6.0_45/include/linux -I~/native/enc? libtsb