473,389 Members | 1,348 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,389 software developers and data experts.

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*);
  6. class edoc
  7. {
  8.     JNIEnv *env;
  9.     JavaVM *jvm;
  11.     HINSTANCE jvmdll;
  12.     JavaVMInitArgs vmargs;
  14. public:
  16. void init()
  17.     {
  18.         env = (JNIEnv*)malloc(sizeof(JNIEnv));
  19.         jvm = (JavaVM*)malloc(sizeof(JavaVM));
  21.         //Load jvm.dll             
  22.         jvmdll = LoadLibrary(JVMDLLPATH);
  24.         //Initialize function variable for JNI_CreateJavaVM
  25.         CreateJavaVM funcCreateJavaVM;            
  26.         funcCreateJavaVM = (CreateJavaVM)GetProcAddress(jvmdll,"JNI_CreateJavaVM");
  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);
  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. };
  47. The managed class looks something like this:
  48. public ref class clrEDOC
  49. {
  50.     edoc* ed;
  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. };
  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 6584
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();
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.     }
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.     }
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

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

Similar topics

by: Alexander Staubo | last post by:
Python does not seem to clean up gracefully on SIGTERM: The exit sequence is not invoked on termination, so the atexit module cannot be used to install shutdown logic. Further, while the signal...
by: Jim McGrail | last post by:
Background: I am investigating a problem involving a windows .NET application that is being developed in C# with Visual Studio 2003. It is a multi-threaded application that uses MSMQ to...
by: Felix Kater | last post by:
The C-faq says that "The malloc/free implementation remembers the size of each block allocated and returned, so it is not necessary to remind it of the size when freeing." Could that length...
by: Ian Sykes | last post by:
I have a C# program which terminates prematurely, with no error or exception being raised, the termination point varying with what diagnostic code is present. The termination point appears to be...
by: JezB | last post by:
I want to clear some specific Session variables when the user closes a page. On my page I have a "return to ..." button which navigates back to the parent page from which it was invoked - under the...
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic...
by: aruna | last post by:
to write a c program to find the roots of the equation using bisection method that too using array or pointers
by: ehabaziz2001 | last post by:
I am facing that error message with no idea WHY the reason ? "Abnormal program termination" E:\programs\c_lang\iti01\tc201\ch06\ownarr01o01 Enter a number : 25 More numbers (y/n)? y...
by: uidzer0 | last post by:
Hey everyone, Taken the following code; is there a "proper" or dynamic way to allocate the length of line? #include <stdio.h> #include <errno.h> int main(int argc, char **argv) { FILE *fp;
by: vsankar9 | last post by:
It's an Service Contracts From CRM. I need termination amount for who's the customer is terminated . End date - 15-DEC-2007 Termination as of date - 15-MAR-2007 Rate - 1000 (monthly rate ) ...
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.