Solved

JNI - creating multiple JVMs

Posted on 2001-07-10
9
1,669 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 3

Accepted Solution

by:
stimpyjcat earned 50 total points
Comment Utility
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 3

Assisted Solution

by:sghosh092199
sghosh092199 earned 50 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
copyEndy  challenge 15 55
scoreUp challenge 14 48
java continue statement 10 72
PDFBox - convert image to PDF, PDF resolution 3 63
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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 how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

743 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now