gpt4 book ai didi

c++ - FindClass 返回 null

转载 作者:行者123 更新时间:2023-11-28 05:15:49 27 4
gpt4 key购买 nike

我的 FindClass 调用有问题,它返回 null:

JData::JIgZorroBridgeClass = env->FindClass(JData::IgZorroBridgePath);

我找到的大多数答案都指向缺少包名称或类加载器问题。

IgZorroBridgePath 这是我的类,具有完全限定的包名称,即。 com/igz/佐罗。另外,如您所见,我在调用 FindClass 之前初始化了 JVM,因此我认为我应该已经在正确的线程上了。

还有什么我可以检查的吗?

#include <assert.h>
#include "JNIHandler.hpp"
#include "JReferences.hpp"

const char* JVMClassPathOption = "-Djava.class.path=Plugin/ig/igplugin-0.1.jar";
const char* IgZorroBridgePath = "com/danlind/igz/ZorroBridge";


void JNIHandler::init()
{
initializeJVM();
initializeJavaReferences();
}

void JNIHandler::initializeJVM()
{
if(isJVMLoaded) return;

JavaVMInitArgs args;
JavaVMOption options[1];

args.version = JData::JNI_VERSION;
args.nOptions = 1;
args.options = options;
options[0].optionString = (char*)JData::JVMClassPathOption;
args.ignoreUnrecognized = JNI_FALSE;

jint res = JNI_CreateJavaVM(&jvm, (void **)&env, &args);
assert(res == JNI_OK);
isJVMLoaded = true;
}

JNIEnv* JNIHandler::getJNIEnvironment(){
return env;
}

JNIEnv* JNIHandler::getEnvForCurrentThread()
{
int envStat = jvm->GetEnv((void **)&env, JData::JNI_VERSION);
if (envStat == JNI_EDETACHED) {
jint res = jvm->AttachCurrentThread((void**)&env, NULL);
assert (res == JNI_OK);
}
return env;
}


void JNIHandler::initializeJavaReferences()
{
if(areJavaReferencesInitialized) return;

initExceptionHandling();
initBridgeObject();
registerNatives();

areJavaReferencesInitialized = true;
}

void JNIHandler::initBridgeObject()
{
JData::JIgZorroBridgeClass = env->FindClass(JData::IgZorroBridgePath);
checkJNIExcpetion(env);
registerClassMethods();
JData::JIgZorroBridgeObject = env->NewObject(JData::JIgZorroBridgeClass, JData::constructor.methodID);
checkJNIExcpetion(env);
}

void JNIHandler::initExceptionHandling()
{
JData::ExceptionClass = env->FindClass(JData::ExcPath);
JData::excGetName.methodID = env->GetMethodID(JData::ExceptionClass, JData::excGetName.name, JData::excGetName.signature);
}

void JNIHandler::registerNatives()
{
JData::JZorroClass = env->FindClass(JData::ZorroPath);
env->RegisterNatives(JData::JZorroClass, JData::nativesTable, JData::nativesTableSize);
checkJNIExcpetion(env);
}

void JNIHandler::registerClassMethods()
{
for(auto *desc : JData::igZorroBridgeMethods)
{
desc->methodID = env->GetMethodID(JData::JIgZorroBridgeClass, desc->name, desc->signature);
checkJNIExcpetion(env);
}
}

void JNIHandler::checkJNIExcpetion(JNIEnv* env)
{
jthrowable exc = env->ExceptionOccurred();
if (!exc) return;

jclass exccls(env->GetObjectClass(exc));
jstring name = static_cast<jstring>(env->CallObjectMethod(exccls, JData::excGetName.methodID));
char const* utfName(env->GetStringUTFChars(name, 0));

JData::excGetMessage.methodID = env->GetMethodID(exccls, JData::excGetMessage.name, JData::excGetMessage.signature);
jstring message = static_cast<jstring>(env->CallObjectMethod(exc, JData::excGetMessage.methodID));
char const* utfMessage(env->GetStringUTFChars(message, 0));

BrokerError(utfName);
BrokerError(utfMessage);

env->ReleaseStringUTFChars(message, utfMessage);
env->ReleaseStringUTFChars(name, utfName);
env->ExceptionClear();
}

最佳答案

我使用的是 spring boot maven 插件,它重新排列了包结构。我改为使用 shade 插件,它不会四处移动包。这使 FindClass 能够找到我的 java 类。

关于c++ - FindClass 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42687500/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com