JNI_OnLoad

The impossible C++ dream:

   jint ret = 1;

   for (auto& cls : classes) {
        ret = 1;
        jclass clazz2 = env->FindClass(cls.c_str());
        if (clazz2) {
            ret = env->RegisterNatives(clazz2,
                        method_table2,
                        sizeof(method_table2) / sizeof(method_table2[0]));
            env->DeleteLocalRef(clazz2);
        } else {
            break;
        }
   }

   return ret == 0 ? JNI_VERSION_1_6 : JNI_ERR;

Pushing kittens:

Lone wolf:

JNI methods:

Power of C++ lambda:

The nice thing with JNI_OnLoad is that the function declaration is now more clearer. I mean, you can opt to keep those directives it will work fine, but you can also no longer need them. So the function declaration/definition now becomes:

Visual Studio 2019 copy dll on Post-Build Event:

Notice carefully, there is a \ slash in certain places there and the output extension (dll or exe) has to be explicitly written.

Microsoft's top 3 sins:

  • Allowing spaces and case insensitivity in file names

  • The use backward slash \ in path separator

  • Their newline endings in text files

I think these sins are unforgivable because firstly, Microsoft first worked on Unix systems in the earliest days so Bill Gates knew this. The strive for user-friendliness is no question and their Visual Studio IDE being light years ahead of the competition is a testament to Microsoft's dedication to user-friendliness in their designs. I guess it's a hit & run case. They did excellent in MSOffice but epic fail in other aspect such as Visual Basic.

Can't blame them it seems there is a primal tension between systems and user-friendliness. People likes to have spaces in names, we also do not care much with regards to case sensitivity. The Unix takes the opposite draconian way.

The best JNI_OnLoad

The JNI_OnLoad allows for a well-organized and flexible mechanism to map native methods in Java to a C method table. The only minimum requirements are:

  • Java native method name and signature must match as in the C method table row

  • Pass the full package class name into the map_JNI function above

Last updated

Was this helpful?