Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the registerNatives() method do?

In java, what does the private static method registerNatives() of the Object class do?

like image 354
Hubris Avatar asked Jun 18 '09 03:06

Hubris


People also ask

What is registerNatives?

Object. registerNatives is an internal method of OpenJDK's implementation of java. lang. Object , called from its class initialiser, and its sole purpose is to register the native methods that pertain to java. lang.

What is JNI and how it works?

JNI is the Java Native Interface. It defines a way for the bytecode that Android compiles from managed code (written in the Java or Kotlin programming languages) to interact with native code (written in C/C++).

What is jmethodID?

jmethodID methodID, va_list args); Constructs a new Java object. The method ID indicates which constructor method to invoke. This ID must be obtained by calling GetMethodID() with <init> as the method name and void ( V ) as the return type.

What is Jclass in JNI?

The jclass instance is your object on which a method will be invoked; you'll need to look up the getName method ID on the Class class, then invoke it on the jclass instance using CallObjectMethod to obtain a jstring result. So in short yes, you just call the getName function and look at the jstring result.


2 Answers

The other answers are technically correct, but not very useful for someone with no JNI experience. :-)

Normally, in order for the JVM to find your native functions, they have to be named a certain way. e.g., for java.lang.Object.registerNatives, the corresponding C function is named Java_java_lang_Object_registerNatives. By using registerNatives (or rather, the JNI function RegisterNatives), you can name your C functions whatever you want.

Here's the associated C code (from OpenJDK 6):

static JNINativeMethod methods[] = {     {"hashCode",    "()I",                    (void *)&JVM_IHashCode},     {"wait",        "(J)V",                   (void *)&JVM_MonitorWait},     {"notify",      "()V",                    (void *)&JVM_MonitorNotify},     {"notifyAll",   "()V",                    (void *)&JVM_MonitorNotifyAll},     {"clone",       "()Ljava/lang/Object;",   (void *)&JVM_Clone}, };  JNIEXPORT void JNICALL Java_java_lang_Object_registerNatives(JNIEnv *env, jclass cls) {     (*env)->RegisterNatives(env, cls,                             methods, sizeof(methods)/sizeof(methods[0])); } 

(Notice that Object.getClass is not in the list; it will still be called by the "standard" name of Java_java_lang_Object_getClass.) For the functions listed, the associated C functions are as listed in that table, which is handier than writing a bunch of forwarding functions.

Registering native functions is also useful if you are embedding Java in your C program and want to link to functions within the application itself (as opposed to within a shared library), or the functions being used aren't otherwise "exported", since these would not normally be found by the standard method lookup mechanism. Registering native functions can also be used to "rebind" a native method to another C function (useful if your program supports dynamically loading and unloading modules, for example).

I encourage everybody to read the JNI book, which talks about this and much more. :-)

like image 87
Chris Jester-Young Avatar answered Oct 12 '22 12:10

Chris Jester-Young


What might be slightly confusing is that the code shown for java.lang.Object.registerNatives in a previous answer is just an example of how to register native functions. This is the code that (in the implementation of OpenJDK) registers native functions for class Object. To register native functions for your own class, you must call the JNI function RegisterNatives from the native code in your own library. This might sound a bit circular, but there are a couple ways to break the loop.

  1. Follow the example of this implementation of class Object:

    a. In your Java class, declare a native method (preferably static) named registerNatives (or any other name. it doesn't matter).

    b. In your native code, define a function named Java_<your fully qualified class name>_registerNatives, which contains a call to the JNI function RegisterNatives.

    c. Make sure that in your Java code, your Java registerNatives method is called prior to any calls to other native methods.

OR

  1. Use JNI_OnLoad

    a. In your native library define a function jint JNI_OnLoad(JavaVM *vm, void *reserved). In the body of this function, call the JNI function RegisterNatives.

    b. The Java VM will automatically look for and call JNI_OnLoad when your native library is loaded by System.loadLibrary, which you should already be calling, probably in a static initializer for your class. (You get the required env pointer by calling the GetEnv function in the table that the vm pointer points to.)

like image 34
Eric Avatar answered Oct 12 '22 11:10

Eric