This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js and have Paul Jensen on-line!
See this thread for details.
Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to stop running multiple instances of jar?  RSS feed

 
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: 24217
38
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!