Agung Wibowo
Posts: 3
Nickname: jdeveloper
Registered: Oct, 2003
|
|
Re: visual c++ and JNI
|
Posted: Oct 1, 2003 7:25 PM
|
|
Thank's for your response but looks this code
i create java class : LoadPicture
and successfully run this program as a replacement of java intepreter to run LoadPicture class.
Next i want to convert this console application into gui win32 application.
i guess just copy the working routine inside main function into callback function behind Button after included "jni.h" but the problem is always arised to functions in jni cannot be recognized by visual c++ itself or function declaration that working properly in console app to be not matched. i don't know if i am wrong in making header configuration setting but i have checked it.
The difficulties from visual c++ are :
how can we initialize JNIEnv *env;
as an actual parameter for function in JNI with declaration resulted from javah -jni our java class command, because to do something in visual c++ to use jni we must use this object before using another jni function. From example i have learnt it automatically used inside main function but in callback function we got problem to refer this object. your helping will be appreciated.
// run on jdk 1.4 // usage invoke jvm javaclassname // Adding Path reference to jvm.dll in jre\bin\client or jre\bin\server directory
#include "jni.h" #include <windows.h>
#ifdef _WIN32 #define PATH_SEPARATOR ';' #else /* UNIX */ #define PATH_SEPARATOR ':' #endif #define USER_CLASSPATH "." /* where Prog.class is */
typedef jint (*P_JNI_GetDefaultJavaVMInitArgs)(void *args); typedef jint (*P_JNI_CreateJavaVM)(JavaVM **pvm, JNIEnv ** penv, void *args);
main(int argc,char** argv) { JNIEnv *env; JavaVM *jvm; // JDK1_1InitArgs vm_args; JavaVMInitArgs vm_args; // jdk 1.2 jint res; jclass cls; jmethodID mid; jstring jstr; jobjectArray args; char classpath[1024]; HANDLE hLib = NULL;
/* Pointer to required functions */ P_JNI_GetDefaultJavaVMInitArgs pfnGetDefaultJavaVMInitArgs = NULL; P_JNI_CreateJavaVM pfnCreateJavaVM = NULL;
/* Load the library */ // printf("Loading Library .... <%s>\n",argv[1]); hLib = LoadLibrary("jvm");
if(hLib == NULL) { printf("Unable to Load Library.... exitting...."); exit(-1); }
/* IMPORTANT: specify vm_args version # if you use JDK1.1.2 and beyond */ //vm_args.version = 0x00010001; vm_args.version = JNI_VERSION_1_2; vm_args.nOptions = 0; vm_args.ignoreUnrecognized = TRUE;
/* Create the Java VM */ /* Store the function pointer for creating the VM */ pfnCreateJavaVM = (P_JNI_CreateJavaVM) GetProcAddress(hLib, "JNI_CreateJavaVM");
/* Create the Java VM */ if(pfnCreateJavaVM != NULL) res = (*pfnCreateJavaVM)(&jvm,&env,&vm_args); if (res < 0) { fprintf(stderr, "Can't create Java VM\n"); exit(1); } else { // printf("Java Virtual Machine created !"); } cls = (*env)->FindClass(env, "LoadPicture"); if (cls == 0) { fprintf(stderr, "Can't find Prog class\n"); exit(1); }
mid = (*env)->GetStaticMethodID(env, cls, "main", "([Ljava/lang/String;)V"); if (mid == 0) { fprintf(stderr, "Can't find Prog.main\n"); exit(1); }
jstr = (*env)->NewStringUTF(env, " from C!");
if (jstr == 0) { fprintf(stderr, "Out of memory\n"); exit(1); }
args = (*env)->NewObjectArray(env, 1, (*env)->FindClass(env, "java/lang/String"), jstr); if (args == 0) { fprintf(stderr, "Out of memory\n"); exit(1); }
(*env)->CallStaticVoidMethod(env, cls, mid, args);
}
|
|