Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Running single instance of the java application

 
satyam vizag narayana
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No issues
Just give a day I will give a sample code for this question
Thanks
 
manoj r patil
Ranch Hand
Posts: 181
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic