-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
  • 305,947
  • 44
  • 307
  • 483
adesh singh
  • 1,727
  • 9
  • 38
  • 70

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
  • 119,121
  • 33
  • 254
  • 329