Is it possible for a JVM to run more than one program at the same time? If so, how? If not, why?
To run a program, we simply do
java ProgramName
But can we use the same JVM instance to run another program?
Is it possible for a JVM to run more than one program at the same time? If so, how? If not, why?
To run a program, we simply do
java ProgramName
But can we use the same JVM instance to run another program?
The answer depends on your definition of "program." The Java programs that have a main
method and that you start with java NameOfClass
typically cannot be run in the same JVM because there's no builtin separation of resources or namespaces. For example, what if the two programs use conflicting versions of the same library?
We also have applications that are designed to share a JVM, such as enterprise applications. These are programs that are designed to run in the context of an "application server", which is basically a program for running other programs. An application server keeps the applications resources separate using classloaders and security managers. For example, two applications may use conflicting versions of a library or conflicting class names and still share a JVM thanks to being loaded through different classloaders.
Not directly. However, since the JVM does support threading, you can paste both programs together by creating a wrapper that starts each one up in a different thread (assuming they're compatible and don't do stuff like define different classes with the same name).
If you don't want the programs to be able to interfere with each other at all or share any state, what's the point of even running them in the same process? If all you want to do is conserve memory by only loading the standard library once, you can set up SecurityManagers
and ClassLoaders
to isolate the two programs. However, this necessarily prevents them from doing crazy stuff with reflection.
If you are executing JAVA command from command prompt, it will always run on separate JVM instance.You can use separate threads for the programs if you want to use the same JVM. But with JAVA command it will be two separate processes, hence separate JVM.
IBM is developing a "multi-tenant" JVM http://www.ibm.com/developerworks/library/j-multitenant-java/ which would make it possible/easier to run multiple Java programs inside the same virtual machine.
One one hand, you cannot. JVM runs only one program. On other hand, the starting program can simply run another programs, sequentially or in parallel. Sequential way is especially simple:
ClassName1.main(new String[]{arg1, arg2});
ClassName2.main(new String[]{arg1, arg2, arg3});
To run java
command you need to use
java [ options ] class [ argument ... ]
java [ options ] -jar file.jar [ argument ... ]
Also from java
- the Java application launcher we can read that
The java tool launches a Java application. It does this by starting a Java runtime environment, loading a specified class, and invoking that class's main method.
So java
creates process for JVM which will load one class and invoke its main method.
I don't see any options to add other classes with for executing its main methods via java
command. If you want you can start new thread/process inside your program, but it wont change the fact that JVM will run only one main method at start.