JavaJNI使用之將JAVA的String[]字串陣列轉化成C語言的char**

餘二五發表於2017-11-23

   用java直接呼叫c裡面的main(int argc,*char[] argv),那麼在java層面上,就需要使用String[]字串陣列,然後將String[]轉化成C語言的char**。

   根據前面的文章,製作呼叫JNI的C標頭檔案:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/* 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中的宣告:

1
2
3
4
5
6
7
8
9
10
11
12
13
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如何實現,其餘兩個類似):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/*
 * 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<len; i++) {
        jstr = (*env)->GetObjectArrayElement(env, strArray, i);
        pstr[i] = (char *)(*env)->GetStringUTFChars(env, jstr, 0);           
               
        }    
            
    HVite(len,pstr);
            
}


   程式碼應該很清楚,不再詳細解釋了。

本文轉自 ponpon_ 51CTO部落格,原文連結:http://blog.51cto.com/liuxp0827/1378277,如需轉載請自行聯絡原作者


相關文章