tags:

views:

122

answers:

2

Hi All, I have created a login page using java swing. and i created jar for the application. Now when I run the jar then my login page is displayed then i minimize the application and again run the jar then another instance of my application is displayed (means now in my system I have two login page. 1 is in minimized format and another is in normal state.

But I want that if in my system login page is already running and is minimized then if i run the jar once again then it will not start as a new application rather it should maximize the earlier login page.

How to achieve this type of functionality ?

please help me

Thanks Sunil Kumar Sahoo

A: 

For a Web Start application, it's easy. You can use the SingleInstanceService and SingleInstanceListener classes. You simply kill off the previous instance when the new instance is ran.

The previous instance should receive some message from the new one by implementing NewActivation(). The previous instance should receive a shut down message and respond accordingly.

The benefit of shutting down the previous instance is that it may have crashed, and bringing the new instance to the foreground is automatic.

Marcus Adams
i am unable to kill the previous instance
Deepak
Rather than kill the previous instance, it may be better to kill the new instance, after notifying the original instance that it should "maximize". It could be as simple as writing to the socket the first instance has open.
Jason Nichols
@Sunil, I provided more details. @Jason, I updated my answer to reflect the benefits of shutting down the previous instance. I like your suggestion, but I've just never implemented it, so I don't know the details on how you "maximize" the application. I've implemented it the other way.
Marcus Adams
A: 

If you want to only have one instance of an application, open up a server socket at some unusual port if you get a failure then the app is already running so exit.

As far as your exact question, it is impossible for a Swing app to know about another one running without doing some specific code.

I guess you could use the Server Port of the app that is running actually listen and perform some commands. The client app could then detect if the server port is unavailable, then open a client and send a command to the port.

Romain Hippeau