If you are running 2 jar files separately, then each jar file runs its own JVM instance and nothing is shared between them. They are 2 seperate process. Full-Stop.
If you wish to communicate between them then here are alternatives:
It depends on what kind of data you wish to transfer.
If it is only Strings, then:
if number of process = 2
and if you are sure of it, then stdin
&8 stdout
is the best way forward. One process can start run another Jar
file by creating a Process
using ProcessBuilder
and then using the streams to communicate. The other process can just do System.out
to transfer message. This is preferred to Socket, because you dont have to handle graceful closing of socket etc. (In case it fails and the port is not un-binded successfully, it can be a big trouble)
if number of jar files > 2
(i.e. number of total processes) and less than
say 10
, you can probably use Sockets and communicate through Socket. This should work well, though extra effort goes in gracefully managing sockets.
if number of process
is Large
, then JMS
should be used. It does a lot of things which you dont need to handle. Too big a task if the number of processes are less.
So in your case, process is the best way forward.
If the data you wish to transfer, can even be Objects. RMI
can be used given the number of processes are less. If more, use JMS
again.
Edit: Now for all the above, there is a lot of dirty work involved. For a change, if you are looking at something new & exciting, I would advice akka. It is a actor based model which communicate with each other using Messages. The beauty is, the actors can be on same JVM or another (very little config) and akka takes care the rest for you. I haven't seen a more cleaner way than doing this :)