?
Solved

JNI - creating multiple JVMs

Posted on 2001-07-10
9
Medium Priority
?
1,812 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 6

Assisted Solution

by:gadio
gadio earned 150 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 150 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 3

Accepted Solution

by:
stimpyjcat earned 150 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 150 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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

770 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