Set the path of jvm.dll to invoke a java method from C using JNI
843811Oct 26 2004 — edited Jan 31 2008Hi ,
I'm trying to call a java method from a C program. it gives no error during compilation as well as building the application. but when i tried to create the JVM by running my application it pops up the message "The application failed to start because jvm.dll was not found. Re-installing the application may fix the problem." I tried out setting all the environment variables to include the jvm.dll(PATH set to c:\j2sdk1.4.2_05\bin;c:\j2sdk1.4.2_05\jre\bin). Still got the same message. Then i re-installed java platform once more. Even now i get the same error. I have more than one jvm.dll at locations jre\bin\client and server, oracle has some jvm.dll . Will that be a problem? if so can i remove those? which of them should be removed and how?
The code i'm using is
#include <stdio.h>
#include <jni.h>
#include <windows.h>
//#pragma comment (lib,"C:\\j2sdk1.4.2_05\\lib\\jvm.lib")
JavaVM jvm; / Pointer to a Java VM */
JNIEnv env; / Pointer to native method interface */
JDK1_1InitArgs vm_args; /* JDK 1.1 VM initialization requirements */
int verbose = 1; /* Debugging flag */
FARPROC JNU_FindCreateJavaVM(char *vmlibpath)
{
HINSTANCE hVM = LoadLibrary("jre\\bin\\server\\jvm.dll");
if (hVM == NULL)
{
return NULL;
}
return GetProcAddress(hVM, "JNI_CreateJavaVM");
}
void main(int argc, char **argv )
{
JavaVM jvm = (JavaVM )0;
JNIEnv env = (JNIEnv )0;
JavaVMInitArgs vm_args;
jclass cls;
jmethodID mid;
jint res;
FARPROC pfnCreateVM;
JavaVMOption options[4];
// jint (__stdcall pfnCreateVM)(JavaVM *pvm, void **penv, void *args) = NULL;
options[0].optionString = "-Djava.compiler=NONE"; /* disable JIT */
options[1].optionString = "-Djava.class.path=c:/j2sdk1.4.2_05/jre/lib/rt.jar"; /* user classes */
options[2].optionString = "-Djava.library.path=lib"; /* set native library path */
options[3].optionString = "-verbose:jni"; /* print JNI-related messages */
/* Setup the environment */
vm_args.version = JNI_VERSION_1_4;
vm_args.options = options;
vm_args.nOptions = 4;
vm_args.ignoreUnrecognized = 1;
JNI_GetDefaultJavaVMInitArgs ( &vm_args );
pfnCreateVM = JNU_FindCreateJavaVM("jre\\bin\\server\\jvm.dll");
res = (*pfnCreateVM)(&jvm,(void **) &env, &vm_args );
// res = JNI_CreateJavaVM(&jvm,(void **) &env, &vm_args );
/* Find the class we want to load */
cls = (*env)->FindClass( env, "InstantiatedFromC" );
if ( verbose )
printf ( "Class: %x" , cls );
/*jvm->DestroyJavaVM( );*/
}
Could anyone help me solve this problem as early as possible, bcoz i'm in an urge to complete the project.
Thanks in advance.
Usha.