Get Strings used in Java from JNI

10,866

The following code should allow you to access the field enrollmentID. Use the JNI String functions to read/manipulate them.

// Load the class
jclass jclass_JCV = env->FindClass(env, "my.package.JC_VerificationCandidate");

jfieldID fid_enrollmentID = env->GetFieldID(env, jclass_JCV, "enrollmentID" , "Ljava/lang/String;");

// Access the first element in the jVerificationCandList array 
jobject jc_v = env->GetObjectArrayElement(env, jVerificationCandList, 0);

// get reference to the string 
jstring jstr = (jstring) env->GetObjectField(env, jc_v, enrollmentID);

// Convert jstring to native string
const char *nativeString = env->GetStringUTFChars(env, jstr, 0);
Share:
10,866
Viktor Apoyan
Author by

Viktor Apoyan

Updated on June 04, 2022

Comments

  • Viktor Apoyan
    Viktor Apoyan almost 2 years

    JAVA Code

    Here is some part of my code that I have written in JAVA, As you can see this is a class called JC_VerificationCandidate that have two String members enrollmentID and seedIndex.

    class JC_VerificationCandidate {
    
        public JCDSM_VerificationCandidate( String enrollmentID, String seedIndex ) {
            this.enrollmentID = enrollmentID;
            this.seedIndex    = seedIndex;
        }
    
        public String enrollmentID;
        public String seedIndex;
    }
    

    Here is main class where I have native method and from where I called that native method.

    public class DsmLibraryTest extends Activity {
        @Override
        public void onCreate(Bundle savedInstanceState) {
    
            JCDSM_VerificationCandidate verificationCandidate[] = {new JCDSM_VerificationCandidate( "tom", "anna" )}; 
            dsm.JDSMVerify( 123456, "http:\\www.test_url.com", bytes, verificationCandidate );
    
        }
    
        public native int JDSMVerify(
                       int                         someValue1,
                       String                      someValue2,
                       byte[]                      someValue3,
                       JC_VerificationCandidate    jVerificationCandList[] );
    }
    

    As you can see I create array with one object and give it to my function.

    JCDSM_VerificationCandidate verificationCandidate[] = {new JCDSM_VerificationCandidate( "tom", "anna" )};
    

    JNI Code

    How can I get that two strings enrollmentID, eedIndex that I have set from java application and which are stored in jVerificationCandList ?

    JNIEXPORT jint JNICALL Java_com_Dsm_Test_DSM_JDSMVerify( JNIEnv* env, jobject thiz, jint jhDevice, jstring jurlID,
                                                             jbyteArray jInputInfo, jobjectArray jVerificationCandList ) {
    
    
    }
    
  • Viktor Apoyan
    Viktor Apoyan almost 13 years
    const char *nativeString = (*env)->GetStringUTFChars(env, jstr, 0); Okay !!! Thats All !!
  • Azeroth2b
    Azeroth2b over 5 years
    Don't forget to release it later, else you will leak. (*env)->ReleaseStringUTFChars(...)