Chinaunix首页 | 论坛 | 博客
  • 博客访问: 88849
  • 博文数量: 29
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 281
  • 用 户 组: 普通用户
  • 注册时间: 2014-01-02 18:00
文章分类
文章存档

2014年(29)

我的朋友

分类: Java

2014-03-18 14:10:34

用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);
            
}


   代码应该很清楚,不再详细解释了。
阅读(6201) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~