I have passed a URL string from Java to C code as jstring
data type through the use of JNI. And my library method needs a char *
as url.
How can I convert jstring
in char *
?
P.S.: Is there any advantage of using jcharArray in C? (i.e. Passing char []
instead of string in native method)
This question is related to
java
java-native-interface
Thanks Jason Rogers's answer first.
In Android && cpp should be this:
const char *nativeString = env->GetStringUTFChars(javaString, nullptr);
// use your string
env->ReleaseStringUTFChars(javaString, nativeString);
Can fix this errors:
1.error: base operand of '->' has non-pointer type 'JNIEnv {aka _JNIEnv}'
2.error: no matching function for call to '_JNIEnv::GetStringUTFChars(JNIEnv*&, _jstring*&, bool)'
3.error: no matching function for call to '_JNIEnv::ReleaseStringUTFChars(JNIEnv*&, _jstring*&, char const*&)'
4.add "env->DeleteLocalRef(nativeString);" at end.
Source: Stackoverflow.com