如何從本機程式碼呼叫 Java 方法
Java Native Interface(JNI) 允許你從本機程式碼呼叫 Java 函式。這是一個如何做到的簡單示例:
Java 程式碼:
package com.example.jniexample;
public class JNITest {
    public static int getAnswer(bool) {
        return 42;
    }
}
原生程式碼:
int getTheAnswer()
{
    // Get JNI environment
    JNIEnv *env = JniGetEnv();
    // Find the Java class - provide package ('.' replaced to '/') and class name
    jclass jniTestClass = env->FindClass("com/example/jniexample/JNITest");
    // Find the Java method - provide parameters inside () and return value (see table below for an explanation of how to encode them) 
    jmethodID getAnswerMethod = env->GetStaticMethodID(jniTestClass, "getAnswer", "(Z)I;");
    // Calling the method
    return (int)env->CallStaticObjectMethod(jniTestClass, getAnswerMethod, (jboolean)true);
}
Java 型別的 JNI 方法簽名:
| JNI 簽名 | Java 型別 | 
|---|---|
| Z | 布林 | 
| B | 位元組 | 
| C | char | 
| S | short | 
| I | int | 
| J | long | 
| F | float | 
| D | double | 
| L fully-qualified-class ; | 完全限定類 | 
| [type | type[] | 
因此,對於我們的示例,我們使用(Z)I - 這意味著函式獲取布林值並返回 int。