aspose file tools*
The moose likes Java in General and the fly likes Running single instance of the java application Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Running single instance of the java application" Watch "Running single instance of the java application" New topic
Author

Running single instance of the java application

satyam vizag narayana
Greenhorn

Joined: Jul 07, 2006
Posts: 8
Hi,

I developed a swing based application in java. I am running it through the executable jar. Each time clicking on the jar file it opens the new instance of the appliction.

But I want to open the single instance of the application i.e. If the application is running , then user tries to open another instance it should be display the error or running application only like yahoomessenger,notepad,etc.

Please tell me how it is done in java. send aly sample code or resources regarding this.

Thanks
in advance
Lucky Chawla
Greenhorn

Joined: Aug 08, 2006
Posts: 22
No issues
Just give a day I will give a sample code for this question
Thanks
manoj r patil
Ranch Hand

Joined: Jun 06, 2002
Posts: 181
There are many ways to achieve this.

One simple way is to create one dummy file in your application root and lock it. You can do this by using FileOutputStream [getChannel().tryLock()]. If you could lock the file, proceed further otherwise throw error to the user that only one instance of this application is allowed!


love your job and not your company;
...because you never know when your company will stop loving you!
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
A common suggestion is to open a ServerSocket on a fixed port number. If you can open it, there is no other instance running. If you get a binding already in use exception, there is another instance running and you should shut down.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Running single instance of the java application