Java Native Interface
lastmod 2023-04-04created 2014-01-0527 edits
The Java Native Interface (JNI) is a programming framework that enables Java code running in a Java Virtual Machine (JVM) to call, and to be called by, native applications (programs specific to a hardware and operating system platform) and libraries written in other languages such as C, C++ and assembly.
Category
- JNI:String: Explanation of JNI String
- JNI:DataByPass: Explanation of how data is passed between JAVA and JNI.
- NDK (Android)
- JNIEnv
Libraries
Examples
- godot-mobile-midi - A mobile MIDI support plugin I'm building myself
- jni_helper.cpp -
lollipop-dev/service/jni/jni_helper.cpp
Features
Explanation of JNI features.
JNI and Threads
When using multiple threads, watch out for the following.
- A JNIEnv pointer is valid only in its own thread.
- Do not pass this JNIEnv pointer to another thread, or cache it and use it in another thread. The JVM passes the same JNIEnv pointer for consecutive function calls in the same thread, but passes a different JNIEnv pointer for function calls from a different thread. So caching JNIEnv in multi thread programming is a mistake.
- A local reference is valid only in the thread that created it.
- Do not pass a local reference to another thread. If you want to share this local reference with another thread, you must turn it into a global reference and share that.
Deleting reference pointers
jobject-related objects obtained with FindClass, NewCharArray, etc. must have their references deleted with the DeleteLocalRef function after use.
JIN_OnLoad
- JNI tips | Android NDK | Android Developers
- JNI and NDK (5) Registering JNI native functions directly
- NDK(4) - JNI_OnLoad example, handling Korean text :: Programming
#include <jni.h>
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) {
JNIEnv *env;
if (vm->GetEnv((void **)&env, JNI_VERSION_1_6) != JNI_OK) {
return JNI_ERR;
}
// env 포인터를 사용하여 Java 메서드 호출 등의 작업을 수행합니다.
return JNI_VERSION_1_6;
}
The Java™ Native Interface: Programmer’s Guide and Specification
- ORACLE: Java Native Interface Specification—Contents
- The Java™ Native Interface - Programmer’s Guide and Specification 2
JNI Functions
When AttachCurrentThread returns -1 in JNI
- When AttachCurrentThread returns -1 in JNI
-
http://docs.hp.com/en/JDKJRE60RN/jdk_rnotes_6.0.01.html#CreateJavaVM -
http://docs.hp.com/en/JAVAPROGUIDE/JNI_java2.html
To use a thread created with pthread in a Native Library from Java (for example, to use a Callback function), you need to get JNIEnv using AttachCurrentThread in a JNI function, but sometimes this fails and -1 is returned. While looking into the cause, I found that the default stack size for threads created with pthread was 256k, and this size seems to be the problem. After setting the stack size to 500k or more with pthread_attr_setstacksize(), the function works properly.
JNIEnv::FindClass does not work inside a created Thread
In Android JNI, the JNIEnv::FindClass method always fails inside a thread created by pthread_create. Googling didn't turn up an exact solution, but according to the hints I found, you need to set the ClassLoader of the corresponding Class. (SEE System.setClassLoader) Since the JNIEnv object is a Thread Local value, it must not be shared globally. To call a JavaClass through JNI from a native thread, you must register the thread with JAVA's MainThread group through JavaVM's AttachCurrentThread or AttachCurrentThreadAsDaemon method. Even so, because of the class loader problem above, to use JNIEnv::FindClass you either register a class loader, or better yet, cache the reference of the corresponding class in advance during initialization (JNI_OnLoad or a separate initialization Routine). However, the jclass reference obtained from FindClass is a LocalReference, so it must be cached as a Global Reference through JNIEnv::NewGlobalRef as follows.
jclass g_myClass; // 전역에서 정의
jint JNI_OnLoad(JavaVM* vm, void* reserved) {
// ... (중략)
g_myClass = reinterpret_cast<jclass>(myEnv->NewGlobalRef(myClass));
Favorite site
- Wikipedia (en) description of JNI
- [Recommended] Implementing callback functions in JNI
- [Recommended] Android JNI callback invocation techniques 3
- [Recommended] Defining the Java Native Interface using JNI_OnLoad (using RegisterNatives)
- JAVA Native Method (JNI) 4
- JNI Tutorial - Using String and Array 5
- How to use JNI6
- Table of contents of the Java Native Interface specification 7
- How JNI works, concepts (NewGlobalRef) 8
- Calling JAVA methods from JNI C/C++ code 9
- JNI Invocation API
References
-
Java_additional_jni_features.pdf ↩
-
JNI_Programmer’s_Guide_and_Specification.pdf ↩
-
How_to_use_JNI_Callback.pdf ↩
-
JAVA_Native_Method_(JNI).pdf ↩
-
JNI_Tutorial_-_String,_Array.pdf ↩
-
How_to_use_JNI.pdf ↩
-
Eureka7_com_ne_kr.zip (see eureka7.com.ne.kr/jdk-1_5_0-Korean-doc/docs/guide/jni/spec/jniTOC.html) ↩
-
Principle_of_operation_JNI.pdf ↩
-
How_to_use_JNI_Callback.pdf ↩