Re: Limit Single Instance
- From: Gordon Beaton <not@xxxxxxxxx>
- Date: 31 Jan 2006 17:57:09 +0100
On Tue, 31 Jan 2006 09:45:34 -0700, Monique Y. Mudama wrote:
> On 2006-01-31, Allan Bruce penned:
>> How can I limit my Java application to be running only once? I want
>> to avoid a user being able to run the application twice, instead if
>> they try to re-open it then it should show the main JFrame of the
>> instance already running. Thanks Allan
>>
>
> The typical way to do this (for any app, not just java) is to check
> for a lock file on startup. If the lock file exists, don't start the
> app. If it doesn't, create one.
The other typical way is to open a ServerSocket listening on a
specific port number. If that fails, you can assume your application
is already running.
This mechanism has two advantages:
- the ServerSocket is automatically released regardless of how the
application terminates, so no false positives are caused by failure
to clean up (other applications or users using the same port number
can still be a problem, so choose wisely).
- a second program instance, upon failing to create the ServerSocket,
can connect to the original and then exit, thereby signalling it to
pop up or move to the front.
/gordon
--
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
.
- Follow-Ups:
- Re: Limit Single Instance
- From: Allan Bruce
- Re: Limit Single Instance
- From: Monique Y. Mudama
- Re: Limit Single Instance
- References:
- Limit Single Instance
- From: Allan Bruce
- Re: Limit Single Instance
- From: Monique Y. Mudama
- Limit Single Instance
- Prev by Date: Re: Event passing in (Swing) GUI Application
- Next by Date: Re: jsp:plugin
- Previous by thread: Re: Limit Single Instance
- Next by thread: Re: Limit Single Instance
- Index(es):
Relevant Pages
|
|