用java直接调用c里面的main(int argc,*char[] argv),那么在java层面上,就需要使用string[]字符串数组,然后将string[]转化成c语言的char**。
根据前面的文章,制作调用jni的c头文件:
/* do not edit this file - it is machine generated */
#include "jni.h"
/* header for class com_xmu_engine_htk */
#ifndef _included_com_xmu_engine_htk
#define _included_com_xmu_engine_htk
#ifdef __cplusplus
extern "c" {
#endif
/*
* class: com_xmu_engine_htk
* method: hcopy
* signature: ([ljava/lang/string;)[ljava/lang/string;
*/
jniexport jobjectarray jnicall java_com_xmu_engine_htk_hcopy
(jnienv *, jclass, jobjectarray);
/*
* class: com_xmu_engine_htk
* method: hvite
* signature: ([ljava/lang/string;)v
*/
jniexport void jnicall java_com_xmu_engine_htk_hvite
(jnienv *, jclass, jobjectarray);
/*
* class: com_xmu_engine_htk
* method: hparse
* signature: ([ljava/lang/string;)v
*/
jniexport void jnicall java_com_xmu_engine_htk_hparse
(jnienv *, jclass, jobjectarray);
#ifdef __cplusplus
}
#endif
#endif
我们看到,jobjectarray就是c的jni定义的string字符串数组。这三个函数在java中的声明:
package com.xmu.engine;
public class htk {
public static native void hcopy(string args[]);
public static native void hvite(string args[]);
public static native void hparse(string args[]);
static {
system.loadlibrary("htkengine");
}
}
下面,重点来看看怎样将java string[]转化按成c的char**(看其中的hvite如何实现,其余两个类似):
/*
* class: com_xmu_engine_htk
* method: hvite
* signature: ()v
*/
jniexport void jnicall java_com_xmu_engine_htk_hvite
(jnienv * env, jclass jc,jobjectarray strarray ){
jstring jstr;
jsize len = (*env)->getarraylength(env, strarray);
char **pstr = (char **) malloc(len*sizeof(char *));
int i=0;
for (i=0 ; i
jstr = (*env)->getobjectarrayelement(env, strarray, i);
pstr[i] = (char *)(*env)->getstringutfchars(env, jstr, 0);
}
hvite(len,pstr);
}
代码应该很清楚,不再详细解释了。
阅读(5981) | 评论(0) | 转发(0) |