468,496 Members | 1,790 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,496 developers. It's quick & easy.

Proper termination of JVM

I'm not sure which section I should post this in, since the problem covers Java, C++ and .NET managed C++...

Anyway - I'm using some Java classes via JNI in unmanaged C++, then call the unmanaged C++ class from managed C++.

What I want to be able to do is create an object that uses JNI, then destroy it, then create and use another.

The problem is that the second time around funcCreateJavaVM returns -1 (unknown error). I figure I'm not disposing it properly the first time, but I'm not sure what I'm doing wrong. The call to DestroyJavaVM is in the destructor of the unmanaged class.

The unmanaged class looks something like this:

Expand|Select|Wrap|Line Numbers
  1. //Path to jvm.dll
  2. #define JVMDLLPATH "C:\\Program Files\\Java\\jdk1.6.0_04\\jre\\bin\\client\\jvm.dll"
  3. //Function type for JNI_CreateJavaVM
  4. typedef UINT (CALLBACK* CreateJavaVM)(JavaVM**,void**,JavaVMInitArgs*);
  5.  
  6. class edoc
  7. {
  8.     JNIEnv *env;
  9.     JavaVM *jvm;
  10.  
  11.     HINSTANCE jvmdll;
  12.     JavaVMInitArgs vmargs;
  13.  
  14. public:
  15.  
  16. void init()
  17.     {
  18.         env = (JNIEnv*)malloc(sizeof(JNIEnv));
  19.         jvm = (JavaVM*)malloc(sizeof(JavaVM));
  20.  
  21.         //Load jvm.dll             
  22.         jvmdll = LoadLibrary(JVMDLLPATH);
  23.  
  24.         //Initialize function variable for JNI_CreateJavaVM
  25.         CreateJavaVM funcCreateJavaVM;            
  26.         funcCreateJavaVM = (CreateJavaVM)GetProcAddress(jvmdll,"JNI_CreateJavaVM");
  27.  
  28.         //Start JVM
  29.         JavaVMOption vmoptions[1];    
  30.         vmargs.version=JNI_VERSION_1_6;
  31.         vmargs.nOptions=2;    
  32.         vmoptions[0].optionString=VMOPTIONCLASSPATH; //capitalized are macros containing paths
  33.         vmoptions[1].optionString=VMOPTIONPATH;
  34.         vmargs.options = vmoptions;
  35.         vmargs.ignoreUnrecognized = JNI_FALSE;            
  36.         funcCreateJavaVM(&jvm, (void**)&env, &vmargs);
  37.  
  38. //multiple calls to JVM via env go here - initialising jclass, jmethodID variables and the like
  39.         }
  40.     ~edoc()
  41.     {
  42.         jvm->DestroyJavaVM();
  43.         FreeLibrary(jvmdll);
  44.     }
  45. };
  46.  
  47. The managed class looks something like this:
  48. public ref class clrEDOC
  49. {
  50.     edoc* ed;
  51.  
  52. public: clrEDOC()
  53.     {
  54.         ed = new edoc();
  55.         ed->init();        
  56.     }
  57. ~clrEDOC()
  58.         {        
  59.             ed->~edoc();            //used to be "delete ed;"
  60.         }
  61. };
  62.  
  63. And the code that calls it is as follows (edocjni is the namespace):
  64. void main()
  65. {
  66.     edocjni::clrEDOC^ clred = gcnew edocjni::clrEDOC(); //executes properly
  67.     clred->~clrEDOC(); //used to be "delete clred;"
  68.     edocjni::clrEDOC^ ed = gcnew edocjni::clrEDOC(); //unknown error while creating JVM
  69. }
Feb 15 '08 #1
3 5916
I may have found a solution myself, but AFAIK its undocumented and I have no idea how good it will work, but the first tests are successful.

What I learned was - the call to DestroyJavaVM does not actually destroy JVM. Quoting from Sun "The VM waits until the main thread is the only user thread before it actually unloads." Why does it not happen immediately, if I have not attached any extra threads to it, I have no idea.

Anyway, until JVM is properly destroyed, another one cannot start in the same thread (hence the error I was getting).

So what I do now, is save the pointer to JNIEnv and submit it as an argument to the function initializing JVM. The beginning of the init method now looks like this:

void init(void* _env)
{
if (_env == NULL) env = new JNIEnv; else env = (JNIEnv*)_env;

I also added a void* getenv() method to get the pointer before disposing the object and removed the DestroyJavaVM call.

Initialized another object and the calls to JVM via the submitted pointer are successful.

Any input on the matter is still appreciated.
Feb 15 '08 #2
For the record: the way I ultimately did that was like this:

Expand|Select|Wrap|Line Numbers
  1. int i = this->getCreatedJVMs();
  2.   if (i>0)
  3.     this->attachtoJVM();
  4.   else
  5.     this->launchJVM();
  6.  
where launchJVM() launches JVM as shown in my first post, attachtoJVM() looks like this:
Expand|Select|Wrap|Line Numbers
  1.     void attachtoJVM()
  2.     {
  3.         dllpath = JVMDLLPATH;
  4.         jvmdll = LoadLibrary(dllpath);
  5.         GetCreatedJavaVMs funcGetCreatedJavaVMs;
  6.         funcGetCreatedJavaVMs = (GetCreatedJavaVMs)GetProcAddress(jvmdll,"JNI_GetCreatedJavaVMs");
  7.         JavaVM **javm;
  8.         int buflen = 4;
  9.         int *nVMs;
  10.         int i = funcGetCreatedJavaVMs(javm, buflen, nVMs);
  11.         javm[0]->AttachCurrentThread((void**)&env,NULL);
  12.     }
  13.  
And getCreatedJVMs() looks like this:
Expand|Select|Wrap|Line Numbers
  1.     //Returns a positive value if at least one JVM is running. 
  2.     //This value can be either the number of JVMs running or the memory address of one of them
  3.     //Behaviour not fully documented.
  4.     int getCreatedJVMs()
  5.     {    dllpath = JVMDLLPATH;
  6.         jvmdll = LoadLibrary(dllpath);
  7.         GetCreatedJavaVMs funcGetCreatedJavaVMs;
  8.         funcGetCreatedJavaVMs = (GetCreatedJavaVMs)GetProcAddress(jvmdll,"JNI_GetCreatedJavaVMs");
  9.         JavaVM **javm;
  10.         int buflen = 4;
  11.         int *nVMs;
  12.         int i = funcGetCreatedJavaVMs(javm, buflen, nVMs);
  13.         if (nVMs==0) return 0; else return (*nVMs);
  14.     }
  15.  
This requires another typedef to work:
Expand|Select|Wrap|Line Numbers
  1. typedef UINT (CALLBACK* GetCreatedJavaVMs)( JavaVM**, int, int* );
Mar 7 '08 #3
Thank You..!
It worked great for me..

You saved my time.
Jun 18 '14 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by Alexander Staubo | last post: by
2 posts views Thread by Jim McGrail | last post: by
35 posts views Thread by Felix Kater | last post: by
3 posts views Thread by JezB | last post: by
669 posts views Thread by Xah Lee | last post: by
9 posts views Thread by ehabaziz2001 | last post: by
9 posts views Thread by uidzer0 | last post: by
reply views Thread by NPC403 | last post: by
3 posts views Thread by gieforce | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.