坏
坏蛋是我
Unregistered / Unconfirmed
GUEST, unregistred user!
我在c++Builder里面调用jni, 如下。。。
struct JavaStruct
{
char *JvmPath;
char *JavaClassPath;
JNIEnv *env;
JavaVM *jvm;
char *SampleClassName;
char *SampleMethodName;
jclass SampleClass;
jmethodID SampleMethod;
};
int LoadSampleMethod(struct JavaStruct *pJavaStruct)
{
jthrowable exc;
jclass j_string;
jclass loaderClass;
char infor[255], *buffer;
char *FileName = "E://JAVA//JniApp//HelloWorld.class";
if(pJavaStruct->jvm == NULL || pJavaStruct->env == NULL) return -1;
TFileStream *FileStream = new TFileStream(FileName, fmOpenRead);
buffer = (char *)malloc(FileStream->Size+2);
FileStream->Read(buffer, FileStream->Size);
loaderClass = pJavaStruct->env->FindClass("java/lang/ClassLoader");
jmethodID loaderMID = pJavaStruct->env->GetMethodID(loaderClass, "<init>", "()V");
if (loaderClass == NULL || loaderMID == NULL) return -1;
jobject loaderObj = pJavaStruct->env->NewObject(loaderClass , loaderMID);
if (loaderObj == NULL) return -2;
pJavaStruct->SampleClass = pJavaStruct->env->DefineClass(pJavaStruct->SampleClassName,
loaderObj, buffer, FileStream->Size);
if( pJavaStruct->SampleClass != 0 )
{
pJavaStruct->SampleMethod = pJavaStruct->env->GetStaticMethodID
(
pJavaStruct->SampleClass,
pJavaStruct->SampleMethodName,
"([Ljava/lang/StringV"
);
}else
{
return -3;
}
free(buffer);
FileStream->Free();
return 0;
}
int _export _stdcall InitJavaStruct(struct JavaStruct *pJavaStruct)
{
LoadJavaVirtualMachineBcb(pJavaStruct);
...
LoadSampleMethod(pJavaStruct);
...
return 0;
}
调试时候, 在LoadSampleMethod函数里面,loaderClass和loaderMID的到的是正确的((非NULL), 但是pJavaStruct->env->NewObject(loaderClass, loaderMID)返回NULL!
为什么?
谢谢!
struct JavaStruct
{
char *JvmPath;
char *JavaClassPath;
JNIEnv *env;
JavaVM *jvm;
char *SampleClassName;
char *SampleMethodName;
jclass SampleClass;
jmethodID SampleMethod;
};
int LoadSampleMethod(struct JavaStruct *pJavaStruct)
{
jthrowable exc;
jclass j_string;
jclass loaderClass;
char infor[255], *buffer;
char *FileName = "E://JAVA//JniApp//HelloWorld.class";
if(pJavaStruct->jvm == NULL || pJavaStruct->env == NULL) return -1;
TFileStream *FileStream = new TFileStream(FileName, fmOpenRead);
buffer = (char *)malloc(FileStream->Size+2);
FileStream->Read(buffer, FileStream->Size);
loaderClass = pJavaStruct->env->FindClass("java/lang/ClassLoader");
jmethodID loaderMID = pJavaStruct->env->GetMethodID(loaderClass, "<init>", "()V");
if (loaderClass == NULL || loaderMID == NULL) return -1;
jobject loaderObj = pJavaStruct->env->NewObject(loaderClass , loaderMID);
if (loaderObj == NULL) return -2;
pJavaStruct->SampleClass = pJavaStruct->env->DefineClass(pJavaStruct->SampleClassName,
loaderObj, buffer, FileStream->Size);
if( pJavaStruct->SampleClass != 0 )
{
pJavaStruct->SampleMethod = pJavaStruct->env->GetStaticMethodID
(
pJavaStruct->SampleClass,
pJavaStruct->SampleMethodName,
"([Ljava/lang/StringV"
);
}else
{
return -3;
}
free(buffer);
FileStream->Free();
return 0;
}
int _export _stdcall InitJavaStruct(struct JavaStruct *pJavaStruct)
{
LoadJavaVirtualMachineBcb(pJavaStruct);
...
LoadSampleMethod(pJavaStruct);
...
return 0;
}
调试时候, 在LoadSampleMethod函数里面,loaderClass和loaderMID的到的是正确的((非NULL), 但是pJavaStruct->env->NewObject(loaderClass, loaderMID)返回NULL!
为什么?
谢谢!