2

Possible duplicate: How to implement a single instance application in Java

There are two methods I know of for creating single instance applications.

  • Listen on a port at 127.0.0.1. This has a problem: if another app using this same method chooses to use the same port, it will not be able to open when my app does.
  • Create a lock file, and either impose a system wide file lock, or write something to the file. There's also a major issue with this one: if the JVM crashes the program may not remove the file locks it previously imposed, thus forcing you to delete the lock file to start the program once more.

Are some of these problems not real? Is there a reliable method of doing such a thing?

I know .NET provides system-wide mutexes, however Java lacks something like this.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
itzjackyscode
  • 970
  • 9
  • 27
  • My understanding is that file locks disappear when the process that owns them terminates. The file may remain, but the lock will not. See https://stackoverflow.com/questions/28900777/is-it-possible-that-a-file-lock-never-gets-released. How is listening on a port insecure? Is any sensitive information is being communicated over the socket? – VGR Dec 06 '20 at 02:00
  • 1
    Maybe the whole file lock not being cleared is something to do with older OSes, after all it was on an old forum – itzjackyscode Dec 06 '20 at 02:05

0 Answers0