无法将DefineClass(JNI)与Qt资源一起使用



我正在尝试使用DefineClass,它是Java本机接口的一部分。 该方法需要一个const jbyte*这是一个const signed char*,我正在尝试从Qt资源解析它。但是一定有问题,因为ExceptionDescribe()打印java.lang.ClassFormatError:截断的类文件

QFile qfile(":/SomeClass.class");
qfile.open(QFile::ReadOnly)
QByteArray qbytes = qfile.readAll();
char *test = qbytes.data();
jbyte *buf = reinterpret_cast<jbyte*>(test);
jclass processorCls = env->DefineClass("example/SomeClass", nullptr, buf, sizeof(buf)); 
if (env->ExceptionCheck())
{
env->ExceptionDescribe();
env->ExceptionClear();
}

我可以验证资源是否有效,因为我能够使用此处找到的方法打印qfile的内容。

void read(QString fName)
{
QFile file(fName);
if (!file.open(QFile::ReadOnly | QFile::Text))
{
std::cout << "Could not open the file for reading" << std::endl;
return;
}
QTextStream in(&file);
QString text = in.readAll();
std::cout << text << std::endl;
file.close();
}

sizeof(buf)返回指针的大小,而不是缓冲区的大小。请改用qbytes.size()

最新更新