Multiple Applications Running On One JVM

Question:
I want to know how to run more than one application under one VM. Iam developing some Java servers, but I don’t want 10 JVMs running whenonly one is necessary. Is there a solution to this?

Answer:
One approach at your disposal is to create a server process whose onlyjob is to load Java classes and execute their main() methods in aseparate thread.

The problem with doing this is that you do not benefit from the separation of address spaces and process contexts that you get from using separate JVMs. If one application thread terminates the application with an errant System.exit(0) or throws a fatal RuntimeException, all your applications will die. In addition, each application will share the same ClassLoader unless you explicitly force each to use a different ClassLoader.

You will want to use a different ClassLoader for each application, otherwise you will runinto problems where applications sharing global state in the form of static variables. Any attempt to share a single JVM between multiple Java applications will in effect lead to implementing your own application server. You could investigate the possibility of usingon of the many available application servers or possibly restructure your applications in the form of servlets.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

Recent Articles: