473,405 Members | 2,279 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,405 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*);
  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 6586
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

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

Similar topics

0
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...
2
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...
35
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...
2
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...
3
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...
669
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...
8
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
9
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...
9
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;
0
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 ) ...
0
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
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...
0
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...
0
marktang
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.