• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to stop running multiple instances of jar?

 
enkhbat dash
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys! I am in trouble. I created program using Java. Exported into JAR. When I started my jar file, program works, ok. But when I try to launch again without closing previous one, then it runs. So how to disable this?

P.S: My English is poor.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24213
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One trick that's often used is for your program to try to open a ServerSocket on a specific port. If it succeeds, then go ahead and run the application. If it fails, then immediately exit. This way, only one copy of your application can run at a time. You can try to do similar things by creating a temporary file and checking for its existence, but this is harder to get right, especially on Windows.
 
enkhbat dash
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So this is the only way. :!:
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!