jni with jstring

If I want to retrieve the int from the java class via jni

I can use

jint execMode = env->GetStaticIntField(cls, env->GetStaticFieldID(cls, "APPLICATION", "I"));

where

public static final int APPLICATION=2


Question is how do I retrieve string from java class ie

public static final String CAP_INJECTION="INJ"


jstring execMode = ???
LVL 26
EDDYKTAsked:
Who is Participating?
 
objectsConnect With a Mentor Commented:
you just need to cast it to a jstring.

> What is different between the function with/without static?

One gets static fields, the other member fields.
0
 
objectsCommented:
env->GetObjectField(o, env->GetFieldID(cls, "CAP_INJECTION", "Ljava/lang/String;"));
0
 
EDDYKTAuthor Commented:
IT throw expection



jstring execMode = (jstring)env->GetStaticObjectField(cls, env->GetStaticFieldID(cls, "CAP_INJECTION", "Ljava/lang/String;")));


I 've tried this but execMode contains jobject instead of jstring

How do I convert from jobject to jstring?

What is different between the function with/without static?
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
EDDYKTAuthor Commented:
I did but when I see the local variable (execMode) from c++ IDE, I onlt see jobject instead of jstring

Do I need to call function to cast from jobject to jstring?
0
 
objectsCommented:
no, just a regular cast should be fine, exactly what error are getting?


0
 
EDDYKTAuthor Commented:
If I use static function

ie

jstring execMode = (jstring)env->GetStaticObjectField(cls, env->GetStaticFieldID(cls, "CAP_INJECTION", "Ljava/lang/String;")));


I've no error but as what I stated

execMode contains jobject instead of jstring
when I see from c++ IDE

how can I extract the string out from jobject?
0
 
EDDYKTAuthor Commented:
I've asked this type of question already. It just long time ago and I've forgetten


jstring execMode = (jstring)env->GetStaticObjectField(cls, env->GetStaticFieldID(cls, "CAP_INJECTION", "Ljava/lang/String;")));

const char *name = env->GetStringUTFChars(execMode, 0);
env->ReleaseStringUTFChars((jstring) execMode, name);





0
 
objectsCommented:
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.