4.写MyDarts_Darts.cpp文件,实现MyDarts_Darts.h中定义的接口,内容如下:
#include <jni.h>
#include "darts.h"
#include "MyDarts_Darts.h"
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <algorithm>
using namespace std;
// the global variable for dictionary
Darts::DoubleArray thedict;
/*
* Class: MyDarts_Darts
* Method: BuildDict
* Signature: (Ljava/lang/String;Ljava/lang/String;)Z
*/
JNIEXPORT jboolean JNICALL Java_MyDarts_Darts_BuildDict
(JNIEnv *env, jclass obj, jstring fileInput, jstring fileDictOutput)
{
// get the file name strings
const char *pFileInput = NULL;
const char *pFileDictOutput = NULL;
pFileInput = env->GetStringUTFChars (fileInput, false);
pFileDictOutput = env->GetStringUTFChars (fileDictOutput, false);
if (pFileInput == NULL || pFileDictOutput == NULL)
return false;
// load the terms and sort
ifstream in (pFileInput);
if (!in)
{
cerr << "Can not open the file of " << pFileInput << endl;
return false;
}
string sTerm;
vector<string> TermVec;
while (getline (in, sTerm))
{
// trim it
if ('\n' == sTerm.at (sTerm.length()-1)
|| '\t' == sTerm.at (sTerm.length()-1))
{
if ('\n' == sTerm.at (sTerm.length()-2)
|| '\t' == sTerm.at (sTerm.length()-2))
sTerm.erase (sTerm.length()-2);
else
sTerm.erase (sTerm.length()-1);
}
TermVec.push_back(sTerm);
}
sort (TermVec.begin(), TermVec.end()); // it's necessary to build dictionary for darts
// make TermVec as a c-style string array, and make the value array
char ** pTermAry = (char **)malloc (TermVec.size() * sizeof(char *));
vector<string>::iterator pIter = TermVec.begin();
int i = 0;
while (pIter != TermVec.end())
{
pTermAry[i] = (char*)pIter->c_str();
pIter++;
i++;
}
// build the dictionary for darts and output it
thedict.build (TermVec.size(), pTermAry, 0, 0);
thedict.save (pFileDictOutput);
// clean up
free (pTermAry);
env->ReleaseStringUTFChars(fileInput, pFileInput);
env->ReleaseStringUTFChars(fileDictOutput, pFileDictOutput);
return true;
}
/*
* Class: MyDarts_Darts
* Method: LoadDict
* Signature: (Ljava/lang/String;)Z
*/
JNIEXPORT jboolean JNICALL Java_MyDarts_Darts_LoadDict
(JNIEnv *env, jclass obj, jstring fileDictInput)
{
// get the file name strings
const char *pFileDictInput = NULL;
pFileDictInput = env->GetStringUTFChars (fileDictInput, false);
if (pFileDictInput == NULL)
return false;
// load the dictionary
if (-1 == thedict.open (pFileDictInput))
{
cerr << "Can not open the file of " << pFileDictInput << endl;
return false;
}
env->ReleaseStringUTFChars(fileDictInput, pFileDictInput);
return true;
}
/*
* Class: MyDarts_Darts
* Method: IsInDict
* Signature: (Ljava/lang/String;)Z
*/
JNIEXPORT jboolean JNICALL Java_MyDarts_Darts_IsInDict
(JNIEnv *env, jclass obj, jstring term)
{
// get the file name strings
const char *pTerm = NULL;
pTerm = env->GetStringUTFChars (term, false);
if (pTerm == NULL)
return false;
int iVal = 0;
thedict.exactMatchSearch (pTerm, iVal);
env->ReleaseStringUTFChars(term, pTerm);
if (-1 == iVal)
return false;
else
return true;
}
仔细看一下,接口中的数据类型都是JNI中的类型,如:jboolean、jstring等等,作为java数据类型和c++数据类型的桥梁,两者能够通过JNI函数相互转换。JNI编程可以查看具体资料,这里不多说。需要提醒一点的是,JNI中获取字符串的函数是GetStringUTFChars,将java String类型的字符串转成c字符串。从函数名字可以看到,它获取\返回的是UTF8编码的字符串,这也是上文强调无论是在构建字典的时候、还是在查询的时候,都要使用UTF8编码字符串的根本原因——不是Darts工具限制,而是JNI有这样的限制。
5.g++生成动态库
命令如下:
g++-I/usr/lib/jvm/java/include-I/usr/lib/jvm/java/include/linuxMyDarts_Darts.cpp -fPIC-shared -o libDarts.so
需要注意两个地方:
a.需要注意的是一定要用‘-I’参数将包含jni.h的路径指明,否则找不到头文件,编译出错。
b.文件名一定要用‘libxxx.so’,其中‘xxx’是动态库的名字,也是代码(无论java还是c++)中load动态库的名字。其余的,‘lib’小写。最常犯的错误是将文件名指定为‘xxx.so’。
至此,libDarts.so文件生成,按照上文的方法就可以正常使用。
