Hey hi. This is sandeep.
I am also trying to do the similar kind of work you are doing, like calling a java method inside c. I have lot of errors. Can you please correct me where i am wrong.
Errors mainly in the lines
(CALLBACK *fpCJV)(JavaVM**, void**, JavaVMInitArgs*);
HINSTANCE hVM;
fpCJV CreateJavaVM;
hVM= LoadLibrary("C:\\Program Files (x86)\\Java\\jre7\\bin\\client\\jvm.dll");
This is whole code. Please somebody help me.
#include <ansi_c.h>
#include <jni.h>
#include <jni_md.h>
#ifdef _WIN32
#define PATH_SEPARATOR ';'
#else
#define PATH_SEPARATOR ':'
#endif
int _stdcall DLLHeader (void)
{
return 1;
}
int main()
{
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 = 0x00010002;
vm_args.options = options;
vm_args.nOptions = 1;
vm_args.ignoreUnrecognized = JNI_TRUE;
#include <ansi_c.h>
#include "jni.h"
#include "jni_md.h"
typedef jint
(CALLBACK *fpCJV)(JavaVM**, void**, JavaVMInitArgs*);
HINSTANCE hVM;
fpCJV CreateJavaVM;
hVM= LoadLibrary("C:\\Program Files (x86)\\Java\\jre7\\bin\\client\\jvm.dll");
if (hVM == NULL)
{
DWORD dwe = GetLastError();
return -1;
}
CreateJavaVM = (fpCJV)GetProcAddress(hVM, "JNI_CreateJavaVM");
status = CreateJavaVM(&jvm, (void**)&env, &vm_args);
if (status != JNI_ERR)
{
cls = (*env)->FindClass(env, "TestClass");
if(cls !=0)
{ mid = (*env)->GetStaticMethodID(env, cls, "intMethod", "(I)I");
if(mid !=0)
{ square = (*env)->CallStaticIntMethod(env, cls, mid, 5);
printf("Result of intMethod: %d\n", square);
}
}
(*jvm)->DestroyJavaVM(jvm);
return 0;
}
else
return -1;
}