Convert JNI types to Native types

Java Native-InterfaceAndroid Ndk

Java Native-Interface Problem Overview


While there is documentation regarding turning a jstring to a native string (string nativeString = env->GetStringUTFChars(jStringVariable, NULL);) I can't find an example which will convert a jboolean to a bool or a jint to an int.

Can anyone suggest how this is achieved?

Java Native-Interface Solutions


Solution 1 - Java Native-Interface

You just need to cast jintto int using C style casts. Same for jboolean to bool (if you're using C99 bool type) or to uint8_t (if you're using std int types) or to unsigned char.

Open $NDK_ROOT/platforms/android-8/arch-arm/usr/include/jni.h and you'll see jint, jboolean etc are just typedefs.

Solution 2 - Java Native-Interface

To cast a jboolean (which may only contain the values JNI_FALSE or JNI_TRUE) to a native bool I would use something like this :

(bool)(jboolean == JNI_TRUE)

If perhaps the jboolean isn't coming from the JVM, then testing for jboolean != JNI_FALSE might be considered safer.

Solution 3 - Java Native-Interface

Same issue–fixed. In my case I'm using openFrameworks so I don't know if this applies to non-openFrameworks projects (haven't tested). However, it appears that the first two arguments in an external function are always "env" and "thiz" and these need to be defined explicitly for each new extern function.

extern "C"{

// casts the variable properly
void Java_com_package_JavaClass_someFunction( JNIEnv*  env, jobject  thiz, jboolean yourBool ){
	myTestApp->someFunction( (bool) yourBool );
}

// "yourBool" will always be "1" because its taking the spot of "thiz" which is not null
void Java_com_package_JavaClass_someFunction( JNIEnv*  env, jboolean yourBool ){
	myTestApp->someFunction( (bool) yourBool );
}

// "yourBool" will always be "1" because its taking the spot of "env" which is not null
void Java_com_package_JavaClass_someFunction( jboolean yourBool ){
	myTestApp->someFunction( (bool) yourBool );
}


}

Solution 4 - Java Native-Interface

The odd one out is jchar. It's defined as unsigned short, and depending on your compilation settings, that may or may not be equivalent to wchar_t. Depending on your underlying platform, you may be better off working with UTF8 strings. At least those are bitwise equivalent to ASCII for the ASCII subset of characters.

On Windows and Mac OS/Cocoa, however, the native wide string representation is exactly unsigned short. Java strings fit naturally into that.

Solution 5 - Java Native-Interface

If you just want to use it in if(..) statement, this is working for me without conversion:

if (jboolean == true) {
  return true;
} else {
  return false;
}

Solution 6 - Java Native-Interface

This has worked for me:

bool isValue = bool(jbooleanValue);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGraemeView Question on Stackoverflow
Solution 1 - Java Native-InterfaceGregory PakoszView Answer on Stackoverflow
Solution 2 - Java Native-InterfaceGuillaume BréhierView Answer on Stackoverflow
Solution 3 - Java Native-InterfaceStevenView Answer on Stackoverflow
Solution 4 - Java Native-InterfaceSeva AlekseyevView Answer on Stackoverflow
Solution 5 - Java Native-Interfacezajac.m2View Answer on Stackoverflow
Solution 6 - Java Native-InterfaceViktor ShabanView Answer on Stackoverflow