I have a simple Java SE application. It creates two instances of the same class, but each one must run in a separate JVM process. How can achieve that?
-
4Start two JVMs, have each create an instance of the class? – Andy Turner Aug 24 '20 at 19:22
-
As Andy Turner says, you need to run java.exe twice to get two JVMs – ControlAltDel Aug 24 '20 at 19:30
-
This requirement is somewhat unusual: inter-process communication is far from simple to set up. If you explained why the instances must be in separate processes we could suggest easier alternatives. – Joni Aug 24 '20 at 19:31
-
Well, is a Java test, I must create two instances of the same class in the same JVM process, then I must create two instances but each one in separate JVM processes, then those instances should exchange some messages, everything just using Java core stuff. – Diego Nieto Aug 24 '20 at 19:42
-
Consider using docker containers for this and orchestrate with docker-compose – Thorbjørn Ravn Andersen Aug 24 '20 at 19:53
-
You have to use thread to do it – ing son Aug 24 '20 at 23:04
-
*"then those instances should exchange some messages"* - Were you told how exactly they were supposed to exchange messages? – Joni Aug 25 '20 at 03:23
-
1Perhaps, [the RMI trail of the tutorial](https://docs.oracle.com/javase/tutorial/rmi/index.html) helps – Holger Aug 25 '20 at 08:10
1 Answers
I have a simple Java SE application, it creates two instances of the same class, but each one must run in separate JVM processes, how can achieve that?
TL;DR:
You cannot achieve this, and you should not be wanting this, as it has no sense at all.
Fetus can not be living in two different mothers' wombs. It has to belong to either.
Little bit more:
When your run a Java program, for each and single Java application (e.g. either just manually packaged .class
files, a .jar
file, J2EE Container, or any other Java application), a discrete instance of JVM spins up on top of your OS kernel.
That JVM instance loads the corresponding bytecode, initializes Class Loader, allocates a memory and CPU time, and finally that's what you see as a running Java application.
If you, however, are talking about the Inter Process Communication and confused this with what you ask in your question, then I have to tell you that IPC is an ability when two (or more) JVM instances communicate between each other, and not to share on Java main class among several JVMs.
As JLS §5.2 says:
The Java Virtual Machine starts up by creating an initial class, which is specified in an implementation-dependent manner, using the bootstrap class loader (§5.3.1). The Java Virtual Machine then links the initial class, initializes it, and invokes the public class method void main(String[]).
Hence, after bootstraping JVM, the main method is invoked after everything is linked and initialized. You can not even theoretically share this among two ore more JVM instances. Think about Class Loader, Garbage Collection, Linking Phase.. which should do what? which should bootstrap the main? which should maintain Class
type instances for object instantiation?
Also, each JVM instance provides single kernel-level Input and Output stream to communicate with OS.
You can, somehow, achieve the vice-versa by running more than one Java applications in one JVM, but that has a lot of implications and problems, and is very much discouraged to do.
Last but not the least: Your System.in
and System.out
kernel-level I/O channels are the only instances from one JVM to the one OS Kernel. Bear this in mind as well, as in case of multiple JVMs running single Java application, this is a complete mess and confusion.

- 9,715
- 8
- 45
- 66
-
-
My answer says that I never heard of them? Strange.. I think it's not about this. Distributed apps don't run under single JVM instance and they don't have a single entry-point method **as well**. If you pay attention OP says that one has a *Java SE Application*, which most likely implies having one application which OP wants to create two instances in different VMs. – Giorgi Tsiklauri Aug 25 '20 at 02:25