정적 메서드 찾기
$ vi FindStaticMethod.cpp#include <jni.h>
#include <iostream>
jint stringToInt
(
JNIEnv *env,
jobject thiz,
jstring str
)
{
//find the java.lang.Integer class
jint ret = -1;
jclass integerClass = env->FindClass("java/lang/Integer");
if(integerClass == nullptr) {
std::cout << "Failed to find the Integer class" << std::endl;
} else {
//find the parseInt static method of the Integer class
jmethodID mid = env->GetStaticMethodID(integerClass, "parseInt", "(Ljava/lang/String;)I");
if(mid == nullptr) {
std::cout << "Failed to find the parseInt static method of Integer" << std::endl;
} else {
//call the method to get a result
ret = env->CallStaticIntMethod(integerClass, mid, str);
}
}
return ret;
}
JNIEXPORT jint JNICALL JNI_OnLoad
(
JavaVM *vm,
void *reserved
)
{
JNIEnv *env;
if(vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6)) {
return -1;
}
JNINativeMethod nm[1] = {
{
const_cast<char*>("stringToInt"),
const_cast<char*>("(Ljava/lang/String;)I"),
reinterpret_cast<void*>(stringToInt)
}
};
jclass cls = env->FindClass("Client");
env->RegisterNatives(cls, nm, 1);
return JNI_VERSION_1_6;
}Last updated