Prevent launching multiple instances of a java application

前端 未结 10 2100
你的背包
你的背包 2020-12-03 03:19

I want to prevent the user from running my java application multiple times in parallel.

To prevent this, I have created a lock file when am opening the application,

10条回答
  •  攒了一身酷
    2020-12-03 03:32

    Similar discussion is at http://www.daniweb.com/software-development/java/threads/83331

    Bind a ServerSocket. If it fails to bind then abort the startup. Since a ServerSocket can be bound only once, only single instsances of the program will be able to run.

    And before you ask, no. Just because you bind a ServerSocket, does not mean you are open to network traffic. That only comes into effect once the program starts "listening" to the port with accept().

提交回复
热议问题