Solved

JNI - creating multiple JVMs

Posted on 2001-07-10
9
1,713 Views
Last Modified: 2013-11-23
Hi,

I'm trying to start 2 JVMs in the same process but each in a different thread. I need to give each one different
arguments (one for debugging, one not debugging).

Unfortunately the second time I call JNI_CreateVM, the value -5 is returned, meaning "JVM already started".

Does anyone have any hints or example code. I'm running Java1.2 on NT, Solaris and Linux.

Thanks in advance,
    Andy.
0
Comment
Question by:andymurd
9 Comments
 
LVL 6

Assisted Solution

by:gadio
gadio earned 50 total points
ID: 6269754
andymurd, as far as I remember the JNI docs specifically indicate that there can only be one JVM per process, so you probably can't do that.
However, why don't you use a single JVM and run through the code twice with different threads? There should be no problems doing that if you didn't use any static resources in your code.
0
 
LVL 1

Author Comment

by:andymurd
ID: 6269807
Thanks Gadio,

I need to run each JVM with different parameters (one has allow a remote debugger to attach, the other must be as fast as possible). AFAIK I can only specify parameters when calling JNI_CreateVM, am I correct?

I've seen lots of complaints on some forums of coders creating too many JVMs (instead of calling AttachThread) so I figured that it should be possible. The JNI also has functions with names like JNI_GetAvailableVMs() so this confused me too.
0
 
LVL 19

Assisted Solution

by:Jim Cakalic
Jim Cakalic earned 50 total points
ID: 6270876
Perhaps you can only create one JVM per native thread? The JNI documentation/tutorial talks about the native thread "bootstrapping" itself into the JVM and "is therefore running just like a native method. The only difference is that there is no concept of returning to the JVM." But then, Essential JNI says that the JNI does not support creating more than one JVM in a single process.

As for JNI_GetAvailableVMs, I think you mean JNI_GetCreatedJavaVMs. Despite the flowery description of its abilities, Essential JNI again states that the current JDK implementation of JNI does not support creating more than one JVM in a single process. Perhaps other vendor JVM implementations do support this? I recall hearing about at least one application server that created multiple VMs in a single native process.

Best regards,
Jim Cakalic
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 3

Accepted Solution

by:
stimpyjcat earned 50 total points
ID: 6272888
I agree with Jim's evaluation.  Even if on some platform you did manage to spawn more than one, I doubt it would be portable.

It certainly should be possible to fork() your process to create another VM, and then use IPC (shared memory or some other mechanism) to communicate between the two portions of your program.
0
 
LVL 3

Assisted Solution

by:sghosh092199
sghosh092199 earned 50 total points
ID: 6274448
Hi!
I believe that you are trying to create the Java VM for the first time as follows:

/* Create the Java VM */
jint res;
JNIEnv *env;
JavaVM *jvm;
JDK1_1InitArgs vm_args;

res = JNI_CreateJavaVM(&jvm,&env,&vm_args);

When you are invoking the JVM for the second time, make sure that the JavaVM address is different i.e
JavaVM *jvm1;

res1 = JNI_CreateJavaVM(&jvm1,&env,&vm_args);

This will help us in coming to a conclusion.




0
 
LVL 3

Expert Comment

by:stimpyjcat
ID: 6275178
Um, sgosh, the JNI_CreateJavaVM takes pointers as its first two arguments so that it can return values in them.  The actual values of the pointers is irrelevant, as long as they are valid memory references.

0
 
LVL 1

Author Comment

by:andymurd
ID: 6276459
I tried creating JVMs in different native threads and got the same results. Forking does work but unfortunately would screw up the rest of my system which is designed to use native threads for client connections.

I guess if it can't be done, it can't be done :-(

I've sent a message to Sun so I'll wait for a response before awarding points.

   Andy.
0
 

Expert Comment

by:CleanupPing
ID: 9061972
andymurd:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
LVL 1

Author Comment

by:andymurd
ID: 9066451
Upgrading to JDK 1.4.1 seemed to fix this problem so I'm splitting the points between gadio, jim_cakalic, sghosh and stimpyjcat.
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

791 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question