posted 18 years ago
I was able to compile:
cl -Ic:\j2sdk1.4.2_10\include -Ic:\j2sdk1.4.2_10\include\win32 -MD Sample2.cpp -FeRunMain.exe -link "C:\j2sdk1.4.2_10\lib\jvm.lib"
Where Sample2.cpp is:
#include <jni.h>
#include <string.h>
#ifdef _WIN32
#define PATH_SEPARATOR ';'
#else
#define PATH_SEPARATOR ':'
#endif
int main()
{
printf("Start C++ application\n");
JavaVMOption options[1];
JNIEnv *env;
JavaVM *jvm;
JavaVMInitArgs vm_args;
long status;
jclass cls;
jmethodID mid;
jint square;
jboolean not;
options[0].optionString = "-Djava.class.path=.";
memset(&vm_args, 0, sizeof(vm_args));
vm_args.version = JNI_VERSION_1_2;
vm_args.nOptions = 1;
vm_args.options = options;
printf("Try to create JVM\n");
status = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args);
printf("JVM function returned");
if (status != JNI_ERR)
{
cls = env->FindClass("Sample2");
if(cls !=0)
{
mid = env->GetStaticMethodID(cls, "intMethod", "(I)I");
if(mid !=0)
{ square = env->CallStaticIntMethod(cls, mid, 5);
printf("Result of intMethod: %d\n", square);
}
mid = env->GetStaticMethodID(cls, "booleanMethod", "(Z)Z");
if(mid !=0)
{ not = env->CallStaticBooleanMethod(cls, mid, 1);
printf("Result of booleanMethod: %d\n", not);
}
}
jvm->DestroyJavaVM();
return 0;
}
else
{
return -1;
}
}
RunMain.exe is generated.
But now I have another problem. Program crashes at line:
status = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args);