JNI Bookmarks

Published: 2020-06-20, Updated: 2020-06-21

Links

jstring to char*

JNIEXPORT void JNICALL Java_ClassName_MethodName(JNIEnv *env, jobject obj, jstring javaString)   
{
   const char *nativeString = env->GetStringUTFChars(javaString, 0);

   // use your string

   env->ReleaseStringUTFChars(javaString, nativeString);
}

Create an object instance

JNIEnv *jni;
jclass clazz = jni->FindClass("java/lang/Integer");
jmethodID method = jni->GetMethodID(clazz, "<init>", "(Ljava/lang/String;)V");
jobject n = jni->NewObject(clazz, method, jni->NewStringUTF("123"));
printf("int created %s\n", n);

JNI Call static method with arguments and return object

JNIEnv *jni;
jclass clazz = jni->FindClass("java/lang/String");
jmethodID method = jni->GetStaticMethodID(clazz, "valueOf", "(Ljava/lang/Object;)Ljava/lang/String;");
if(method == NULL){
  jni->FatalError("Class method not found\n");
}
jstring arg1 = jni->NewStringUTF("Hello World!!!");
jstring result = (jstring) jni->CallStaticObjectMethod(clazz, method, arg1);
jni->ReleaseStringUTFChars(arg1, NULL);
if (result == NULL) {
  jni->FatalError("couldn't call method");
}
jboolean isCopy = (jboolean) false;
printf("result: %s\n", jni->GetStringUTFChars(result, &isCopy));

To find a method signature from a specific class

$ javap -v java.lang.Class | grep -A1 'forName('

public static java.lang.Class<?> forName(java.lang.Module, java.lang.String);
  descriptor: (Ljava/lang/Module;Ljava/lang/String;)Ljava/lang/Class;

UnsatisfiedLinkError only in 32 bit shared library

Add link options, cmake example

-Wl,--add-stdcall-alias -m32 -shared

jni commands, jni bookmarks, jvmti commands, jvmti bookmarks


Compilando C/C++ com Gradle Minecraft Dissasembler

Comments