I'm interested on running multiple program within a single JVM. for a instance, let say, I want to run two instances of same hello world program in a one JVM. I would like to know it is possible and if it possible I'm expecting a resource to learn how to do it by myself.
The public static void main(String) method is in the end still a method. You can call it like any other static method, provided you give the required arguments. Running it in a separate thread is probably a good idea.
There is one thing to be careful about. If the program calls System.exit that will not only exit that program but also all other programs running from your launcher. This also includes user interface frames with Frame.EXIT_ON_CLOSE set as the default close operations.
Be aware that multiple applications running within a single JVM would share static variables, singletons etc. That's why application servers separate applications through the use of classloaders, and why they normally associate security managers with each application (or application classloader), so that applications can't interfere with one another. Calls to System.exit() which Rob mentioned are just one example of this.
Nuwan Arambage wrote:Is there a mechanism to run two instances of the same program within a single JVM. If it is possible, how can we do it..
Rob answered that - you can call the main method. But as both Rob and I mentioned, there may be problems if the applications aren't aware that other application instances may be running in the same JVM.
Nuwan wrote:I didn't get what you guys are talking about.. I do have a clear question... Is there a mechanism to run two instances of the same program within a single JVM. If it is possible, how can we do it..
I'm looking for a solid answer.. this question intrigues me a lot..
Why that question intrigues you a lot?
Read the posts from Ulf & Rob again they already given answer to your question.