Skip to content
Search

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

Libraries

Examples

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

#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

JNI Functions

When AttachCurrentThread returns -1 in JNI

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

References


  1. Java_additional_jni_features.pdf ↩

  2. JNI_Programmer’s_Guide_and_Specification.pdf ↩

  3. How_to_use_JNI_Callback.pdf ↩

  4. JAVA_Native_Method_(JNI).pdf ↩

  5. JNI_Tutorial_-_String,_Array.pdf ↩

  6. How_to_use_JNI.pdf ↩

  7. Eureka7_com_ne_kr.zip (see eureka7.com.ne.kr/jdk-1_5_0-Korean-doc/docs/guide/jni/spec/jniTOC.html) ↩

  8. Principle_of_operation_JNI.pdf ↩

  9. How_to_use_JNI_Callback.pdf ↩

author Zer0 sitemap