User
User

Reputation: 1373

How can two instances of an application communicate in 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?

Upvotes: 4

Views: 1394

Answers (3)

Riduidel
Riduidel

Reputation: 22308

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.

Upvotes: 7

tim_yates
tim_yates

Reputation: 171184

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

Upvotes: 3

thelost
thelost

Reputation: 6694

You could use ports.

Upvotes: 1

Related Questions