Solved

JNI - creating multiple JVMs

Posted on 2001-07-10
9
1,699 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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

773 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