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
Joined: Aug 08, 2006
No issues Just give a day I will give a sample code for this question Thanks
manoj r patil
Joined: Jun 06, 2002
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!
Joined: Jan 29, 2003
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