-1

I am working on Java Swing application . I have created the jar of my application , when user clicks on the jar application starts. If user clicks multiple times on the jar then multiple instances of the jar run.

SO how can i stop multiple instances of running of Java jar file?

user207421
  • 298,294
  • 41
  • 291
  • 462
adesh singh
  • 1,690
  • 9
  • 38
  • 69

1 Answers1

0

Choose a location where to create a lock directory. On startup, try and create it: if it fails, you know another instance is already running.

When the program shuts down, delete the directory.

Note: a directory and not a file, since directory creation is atomic and Java doesn't have O_CREAT|O_EXCL, except for Java 7+.

fge
  • 114,841
  • 28
  • 237
  • 319