Java JNI:如何从C ++的本机方法设置Java类的String []字段

时间:2016-11-10 09:59:21

标签: java c++ c java-native-interface native

我有这个课程: ProcessSolution

public class ProcessSolution {
    private String processId;
    private String processName;
    private String processSolutionSteps [];
    private String processRemark;
    private String processNote;
}

我想从我的原生方法

设置其中一个字段processSolutionSteps

此处step_name除了processSolutionSteps

之外什么都没有
JNIEXPORT jobjectArray JNICALL Java_nativeclass_Jmain_getProcessSolutions
  (JNIEnv *env, jobject jobject) {
    clientlist *cl = new clientlist();
    jmp_buf jmpb;
    int i, j , k;
    int listCount = 0, steps = 0;
    jobjectArray arrPS = NULL;
    jobject clsObj;
    struct process *list = NULL;
    struct procdtl *pdtl;
    char *remark, *note, *step_name;

    list = (struct process *)calloc (sizeof(process)  , 1);
    pdtl = (struct procdtl *)calloc (sizeof(procdtl)  , 1);
    remark = (char*) calloc(201 ,1);
    note = (char*) calloc(201 ,1);
    step_name = (char*) calloc(201 ,1);

    cl->get_proc_list ( &listCount, &list );

    jclass clsPS = env->FindClass("POJO/ProcessSolution");
    if( clsPS == NULL )
        return NULL;
    if( ( arrPS = env->NewObjectArray( listCount, clsPS, NULL ) ) == NULL );
        longjmp(jmpb,-1);
    for ( i = 0; i < listCount; ++i ) {
            remark = get_proc_remark (list[i].proc_id);
            note = get_proc_note (list[i].proc_id);
            get_proc_list (list[i].proc_id ,  &steps, &pdtl);

            for (j = 0; j < steps; j++) {
                memset(step_name, ' ', 200);
                strcpy(step_name, pdtl[j].desc[0]);
                if (data_in(ps.desc[1]))
                    sprintf(step_name+strlen(step_name), "%s", pdtl[j].desc[1]);
                if (data_in(ps.desc[2]))
                    sprintf(step_name+strlen(step_name), "%s", pdtl[j].desc[2]);
                if (data_in(ps.desc[3]))
                    sprintf(step_name+strlen(step_name), "%s",pdtl[j].desc[3]);
                if (data_in(ps.desc[4]))
                    sprintf(step_name+strlen(step_name), "%s", pdtl[j].desc[4]); 
            }

    }


}

我的问题是如何从c ++方法中设置processSolutionSteps []

谢谢

1 个答案:

答案 0 :(得分:1)

jfieldID procSolStepsID = env->GetFieldID(clsPS, "processSolutionSteps", "[Ljava/lang/String;");
jclass clsStr = env->FindClass("java/lang/String");
for (i = 0; i < listCount; ++i) {
    jobject procSolObj = env->NewObject(clsPS, env->GetMethodID(clsPS, "<init>", "()V"));
    jobjectArray solSteps = env->NewObjectArray(steps, clsStr, NULL);
    for (j = 0; j < steps; ++j) {
        // prepare step_name
        env->SetObjectArrayElement(solSteps, j, env->NewStringUTF(step_name));
    }
    env->SetObjectField(procSolObj, procSolStepsID, solSteps);
    env->SetObjectArrayElement(arrPS, i, procSolObj);
}