This discussion is archived
1 Reply Latest reply: Sep 19, 2011 5:36 PM by EJP RSS

HELP!  JNI C++

889079 Newbie
Currently Being Moderated
#include "stdafx.h"

#include <windows.h>
#include "jni.h"

#include <stdlib.h>
#include <stdio.h>
#include <string.h>


int main(int argc, char* argv[])
{
     
     printf("Hello World!\n");

     int res;  
 JavaVM *jvm;  
 JNIEnv *env;  
 JavaVMInitArgs vm_args;  
 JavaVMOption options[3];  
 /*设置初始化参数*/  
 options[0].optionString = "-Djava.compiler=NONE";   
 //options[1].optionString = "-Djava.class.path=.";   
 //options[2].optionString = "-verbose:jni"; //用于跟踪运行时的信息  

 /*版本号设置不能漏*/  
 vm_args.version = JNI_VERSION_1_4;//jdk版本1.2 
 vm_args.nOptions = 1;  
 vm_args.options = options;  
 vm_args.ignoreUnrecognized = JNI_TRUE;  
 res = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args);  
 if (res < 0) {  
  fprintf(stderr, "Can't create Java VM/n");  
  exit(1);  
 } 

     //////////////LJX _测试 jobjectArray使用方法
    jobjectArray ret;
    int i;

     char bbb[4] = {"a",};
    char *message[5]= {"first", 
     "second", 
     "third", 
     "fourth", 
     "fifth"};

    ret= (jobjectArray)env->NewObjectArray(5,
         env->FindClass("java/lang/String"),
         env->NewStringUTF(""));

    for(i=0;i<5;i++) {
          printf("message%d:%s\n", i, env->NewStringUTF(message));
          printf("message%d:%s\n", i, message[i]);
env->SetObjectArrayElement(ret,i,env->NewStringUTF(message[i]));
}

     jbyte* b = env->GetByteArrayElements((jbyteArray)ret,NULL);
printf("b:%s---%s----%s\n",b,&b,(char*)b);
     for(i=0;i<5;i++)
     {
     //char* b = &t[i] ;
     printf("b1%d:%s\n", i, &b[i]);
     // printf("b2%d:%s\n", i, env->NewStringUTF(b[i]));
     //printf("arr%d:%s\n", i,arr[i]);
     }

     //////////////LJX _测试 jobjectArray使用方法

     return 0;
}
result:

Hello World!
message0:(?
message0:first
message1:埗
message1:second
message2:瓒
message2:third
message3:H?
message3:fourth
message4:ǚ
message4:fifth
b:X??;---玄;----X??;
b10:X??;
b11:??;
b12:?;
b13:?;
b14:?;
Press any key to continue


why?  

print  {"first", 
     "second", 
     "third", 
     "fourth", 
     "fifth"}

Edited by: EJP on 20/09/2011 07:51: added code tags. Please use them.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
  • 1. Re: HELP!  JNI C++
    EJP Guru
    Currently Being Moderated
    jobjectArray ret;
    So 'ret' is a jobjectArray.
    ret= (jobjectArray)env->NewObjectArray(5,
    env->FindClass("java/lang/String"),
    env->NewStringUTF(""));
    Still a jobjectArray.
    env->SetObjectArrayElement(ret,i,env->NewStringUTF(message));
    Still a jobjectArray.
         jbyte* b = env->GetByteArrayElements((jbyteArray)ret,NULL);
    So it isn't a jbyteArray. So don't cast it to one, and don't call methods on it that presume it is one. You're lucky this didn't crash the JVM.
    why?
    Because your code doesn't make any sense. You created an object array of Strings, i.e. a String[], then you tried to treat it as a jbyte*, which it isn't.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points