14

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?

CodeBlue
  • 14,631
  • 33
  • 94
  • 132

6 Answers6

14

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.

Joni
  • 108,737
  • 14
  • 143
  • 193
  • 2
    To my understanding OSGi solves the separation of jars in different classloaders. – Thorbjørn Ravn Andersen Oct 22 '13 at 17:55
  • I wouldn't close out simply to implement a `.jar`, whose `static main()` simply loads multiple jars and calls their `static main()` on different threads. Yes, it would be problematic on namespace/resource collisions, but mainly it would work. – peterh Jan 20 '17 at 15:00
4

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.

Antimony
  • 37,781
  • 10
  • 100
  • 107
3

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.

CoderP
  • 1,361
  • 1
  • 13
  • 19
2

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.

Rob Stoecklein
  • 749
  • 6
  • 9
1

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});
Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417
Alexei Kaigorodov
  • 13,189
  • 1
  • 21
  • 38
0

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.

Community
  • 1
  • 1
Pshemo
  • 122,468
  • 25
  • 185
  • 269