Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can two instances of an application communicate in Java?

Tags:

java

I am developing a new Java Desktop app. Something like a media player. I want to load most of the resources in the background when the computer starts up. But the users can turn this option off form within the app or using some other utility. So, what I want to do is if a ban instance of the app is already running and the user starts the app again then I can communicate with the already running instance so that it can launch a new window?

like image 406
User Avatar asked Dec 23 '22 02:12

User


2 Answers

The most known way to do that is to open a ServerSocket when first application starts on a well known port.

If ServerSocket fails to load, it's probably because an instance is already running.

In such a case, you can open a Socket and start to communicate your orders between both instances.

But you can also use far more sophisticated solutions, like Jini or JGroups.

like image 76
Riduidel Avatar answered Dec 28 '22 11:12

Riduidel


Write the app so it has a server part

When it starts up, try to communicate to the server (if it is already running), and if that works, then the server should open a new window, and the client should die

This should give you an overview:

http://java.sun.com/docs/books/tutorial/networking/sockets/clientServer.html

like image 45
tim_yates Avatar answered Dec 28 '22 11:12

tim_yates