How do I make sure only one instance of my program can be executed?

后端 未结 6 681
旧时难觅i
旧时难觅i 2021-01-07 01:17

I want my program, a Java executable .jar, to be run just once. I made a program but now I want users not to be able to open more than one instance ....thanks for your time.

6条回答
  •  盖世英雄少女心
    2021-01-07 01:58

    You can use a lock file solution. On startup of the application, have it check for a specific file. If it doesn't exist, create it and start the application normally. If it does exist, exit the application. You need to ensure the file is deleted when the application shuts down (maybe using a FileLock).

提交回复
热议问题