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"));


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
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mick BarryJava DeveloperCommented:
env->GetObjectField(o, env->GetFieldID(cls, "CAP_INJECTION", "Ljava/lang/String;"));
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?
Mick BarryJava DeveloperCommented:
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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Why Diversity in Tech Matters

Kesha Williams, certified professional and software developer, explores the imbalance of diversity in the world of technology -- especially when it comes to hiring women. She showcases ways she's making a difference through the Colors of STEM program.

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?
Mick BarryJava DeveloperCommented:
no, just a regular cast should be fine, exactly what error are getting?

EDDYKTAuthor Commented:
If I use static function


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?
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);

Mick BarryJava DeveloperCommented:
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.