What is jni in java example
A native method can even update Java objects that it created or that were passed to it, and these updated objects are available to the Java application. Thus, both the native language side and the Java side of an application can create, update, and access Java objects and then share these objects between them. Native methods can also easily call Java methods. Often, you will already have developed a library of Java methods. Your native method does not need to "re-invent the wheel" to perform functionality already incorporated in existing Java methods.
The native method, using the JNI framework, can call the existing Java method, pass it the required parameters, and get the results back when the method completes. Your native functions all receive a JNIEnv as the first argument.
The JNIEnv is used for thread-local storage. For this reason, you cannot share a JNIEnv between threads. Assuming it has one; see AttachCurrentThread below. The "jni. For this reason it's a bad idea to include JNIEnv arguments in header files included by both languages. All threads are Linux threads, scheduled by the kernel. They're usually started from managed code using Thread. It's usually best to use Thread. Doing so will ensure that you have sufficient stack space, that you're in the correct ThreadGroup , and that you're using the same ClassLoader as your Java code.
Attaching a natively-created thread causes a java. Thread object to be constructed and added to the "main" ThreadGroup , making it visible to the debugger. Calling AttachCurrentThread on an already-attached thread is a no-op.
Android does not suspend threads executing native code. If garbage collection is in progress, or the debugger has issued a suspend request, Android will pause the thread the next time it makes a JNI call. If coding this directly is awkward, in Android 2. Similarly, to call a method, you'd first get a class object reference and then a method ID. The IDs are often just pointers to internal runtime data structures. Looking them up may require several string comparisons, but once you have them the actual call to get the field or invoke the method is very quick.
If performance is important, it's useful to look the values up once and cache the results in your native code. Because there is a limit of one JavaVM per process, it's reasonable to store this data in a static local structure.
The class references, field IDs, and method IDs are guaranteed valid until the class is unloaded. Classes are only unloaded if all classes associated with a ClassLoader can be garbage collected, which is rare but will not be impossible in Android.
Note however that the jclass is a class reference and must be protected with a call to NewGlobalRef see the next section. If you would like to cache the IDs when a class is loaded, and automatically re-cache them if the class is ever unloaded and reloaded, the correct way to initialize the IDs is to add a piece of code that looks like this to the appropriate class:.
The code will be executed once, when the class is initialized. If the class is ever unloaded and then reloaded, it will be executed again. Every argument passed to a native method, and almost every object returned by a JNI function is a "local reference". This means that it's valid for the duration of the current native method in the current thread. Even if the object itself continues to live on after the native method returns, the reference is not valid.
This applies to all sub-classes of jobject , including jclass , jstring , and jarray. The runtime will warn you about most reference mis-uses when extended JNI checks are enabled. If you want to hold on to a reference for a longer period, you must use a "global" reference.
The NewGlobalRef function takes the local reference as an argument and returns a global one. The global reference is guaranteed to be valid until you call DeleteGlobalRef. This pattern is commonly used when caching a jclass returned from FindClass , e. All JNI methods accept both local and global references as arguments. It's possible for references to the same object to have different values.
For example, the return values from consecutive calls to NewGlobalRef on the same object may be different. To see if two references refer to the same object, you must use the IsSameObject function. One consequence of this is that you must not assume object references are constant or unique in native code. The bit value representing an object may be different from one invocation of a method to the next, and it's possible that two different objects could have the same bit value on consecutive calls.
Do not use jobject values as keys. Programmers are required to "not excessively allocate" local references. In practical terms this means that if you're creating large numbers of local references, perhaps while running through an array of objects, you should free them manually with DeleteLocalRef instead of letting JNI do it for you.
Please send us your feedback if you run into any problems we may have overlooked. As of Java SE 6. Skip to Content. Java Native Interface Overview. Java Native Interface Approach. Programming to the JNI. The standard Java class library does not support the platform-dependent features needed by the application.
You already have a library written in another language, and wish to make it accessible to Java code through the JNI. You want to implement a small portion of time-critical code in a lower-level language such as assembly. Create, inspect, and update Java objects including arrays and strings. Each VM vendor can support a larger body of native code. Tool builders will not have to maintain different kinds of native method interfaces.
Application programmers will be able to write one version of their native code and this version will run on different VMs. Binary compatibility - The primary goal is binary compatibility of native method libraries across all Java VM implementations on a given platform. Programmers should maintain only one version of their native method libraries for a given platform. Efficiency - To support time-critical code, the native method interface must impose little overhead. It adapts Java method signatures to native function prototypes jni.
JNI data type mapping in variables. Create a Java class with native method s : public native void sayHi String who, int times ; 2. Load the library which implements the method: System. Invoke the native method from Java For example, our Java code could look like this:.
0コメント