X
xintianxia_1976
Unregistered / Unconfirmed
GUEST, unregistred user!
请问大家一个是使用c++调用java
在c++程序中要先进行jvm的创建,可是一直存在问题,jdk使用1.4版本
程序如下:
BOOL CInitJavaVM()
{
char*szClassPath;
char*libvar;
intlen;
HGLOBALhMem;
JavaVMInitArgsvm_args;
JavaVMOptionoptions[4];
jint jIntRet = -1;
HINSTANCE hDLL= NULL;
AttachCurrentThreadAsDaemonpfnCreateJavaVM = NULL;
memset(&vm_args, 0, sizeof(JavaVMInitArgs));
hDLL = LoadLibrary("JVM");
if(hDLL == NULL) {
MessageBox(NULL, "Can't Load VM Library! ", "Error", MB_OK);
return FALSE;
}
libvar = getenv("CLASSPATH");
len = strlen(libvar);
hMem = GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT, len + CLASSHEADLEN);
if(hMem == NULL){
MessageBox(NULL, "Out of memory!", "Error", MB_OK);
return FALSE;
}
szClassPath = (char*)GlobalLock(hMem);
if(szClassPath == NULL){
return FALSE;
}
sprintf(szClassPath, "%s%s%c%s", CLASSHEAD, libvar, PATH_SEPARATOR, USER_CLASSPATH);
options[0].optionString = "-Djava.compiler=NONE";
options[1].optionString = szClassPath;
options[2].optionString = "-Djava.library.path=.";options[3].optionString = "-verbose:jni";
vm_args.version = JNI_VERSION_1_4;
vm_args.options = options;
vm_args.nOptions = 4;
vm_args.ignoreUnrecognized = JNI_TRUE;
pfnCreateJavaVM = (AttachCurrentThreadAsDaemon) GetProcAddress(hDLL, "JNI_CreateJavaVM");
jIntRet = (*pfnCreateJavaVM)(&JVM, &JniEnv, &vm_args);
pfnCreateJavaVM = (AttachCurrentThreadAsDaemon) GetProcAddress(hDLL, "JNI_CreateJavaVM");
if((pfnCreateJavaVM != NULL) &&
(!bCVMSuccess)){
jIntRet = (*pfnCreateJavaVM)(&JVM, &JniEnv, &vm_args);
}
GlobalUnlock(hMem);
GlobalFree(hMem);
if(jIntRet = JNI_ERR ){
MessageBox(NULL, "Can't Create VM!!!", "Error", MB_OK);
return FALSE;
}
bCVMSuccess = TRUE;
jTcabClass = JniEnv->FindClass("TcabJavaClient");
if (jTcabClass == 0) {
MessageBox(NULL, "Can't Find TcabJavaClient Class!", "Error", MB_OK);
return FALSE;
}
jTcabClass = (jclass)JniEnv->NewGlobalRef(jTcabClass);
if (jTcabClass == 0) {
MessageBox(NULL, "Can't Create Global Reference for TcabJavaClient Class!", "Error", MB_OK);
return FALSE;
}
FreeLibrary(hDLL);
return TRUE;
}
错误是创建jvm失败,请问哪位大虾知道愿意,多谢多谢
在c++程序中要先进行jvm的创建,可是一直存在问题,jdk使用1.4版本
程序如下:
BOOL CInitJavaVM()
{
char*szClassPath;
char*libvar;
intlen;
HGLOBALhMem;
JavaVMInitArgsvm_args;
JavaVMOptionoptions[4];
jint jIntRet = -1;
HINSTANCE hDLL= NULL;
AttachCurrentThreadAsDaemonpfnCreateJavaVM = NULL;
memset(&vm_args, 0, sizeof(JavaVMInitArgs));
hDLL = LoadLibrary("JVM");
if(hDLL == NULL) {
MessageBox(NULL, "Can't Load VM Library! ", "Error", MB_OK);
return FALSE;
}
libvar = getenv("CLASSPATH");
len = strlen(libvar);
hMem = GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT, len + CLASSHEADLEN);
if(hMem == NULL){
MessageBox(NULL, "Out of memory!", "Error", MB_OK);
return FALSE;
}
szClassPath = (char*)GlobalLock(hMem);
if(szClassPath == NULL){
return FALSE;
}
sprintf(szClassPath, "%s%s%c%s", CLASSHEAD, libvar, PATH_SEPARATOR, USER_CLASSPATH);
options[0].optionString = "-Djava.compiler=NONE";
options[1].optionString = szClassPath;
options[2].optionString = "-Djava.library.path=.";options[3].optionString = "-verbose:jni";
vm_args.version = JNI_VERSION_1_4;
vm_args.options = options;
vm_args.nOptions = 4;
vm_args.ignoreUnrecognized = JNI_TRUE;
pfnCreateJavaVM = (AttachCurrentThreadAsDaemon) GetProcAddress(hDLL, "JNI_CreateJavaVM");
jIntRet = (*pfnCreateJavaVM)(&JVM, &JniEnv, &vm_args);
pfnCreateJavaVM = (AttachCurrentThreadAsDaemon) GetProcAddress(hDLL, "JNI_CreateJavaVM");
if((pfnCreateJavaVM != NULL) &&
(!bCVMSuccess)){
jIntRet = (*pfnCreateJavaVM)(&JVM, &JniEnv, &vm_args);
}
GlobalUnlock(hMem);
GlobalFree(hMem);
if(jIntRet = JNI_ERR ){
MessageBox(NULL, "Can't Create VM!!!", "Error", MB_OK);
return FALSE;
}
bCVMSuccess = TRUE;
jTcabClass = JniEnv->FindClass("TcabJavaClient");
if (jTcabClass == 0) {
MessageBox(NULL, "Can't Find TcabJavaClient Class!", "Error", MB_OK);
return FALSE;
}
jTcabClass = (jclass)JniEnv->NewGlobalRef(jTcabClass);
if (jTcabClass == 0) {
MessageBox(NULL, "Can't Create Global Reference for TcabJavaClient Class!", "Error", MB_OK);
return FALSE;
}
FreeLibrary(hDLL);
return TRUE;
}
错误是创建jvm失败,请问哪位大虾知道愿意,多谢多谢