Solved

JNI - creating multiple JVMs

Posted on 2001-07-10
9
1,737 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java: How do I open the default windows program for a given file type 3 53
ejb on wildfly 5 45
Glassfish admin console not working 1 54
jsp error 6 42
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

685 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