How to make sure that only a single instance of a Java application is running?

前端 未结 11 1732
你的背包
你的背包 2020-12-09 20:23

I want my application to check if another version of itself is already running.

For example, demo.jar started, user clicks to run it again, but the sec

11条回答
  •  一个人的身影
    2020-12-09 20:43

    Contrary to several other answers, the most reliable method is to create a ServerSocket on a fixed port known only to you, way up in the paint cards. It will automatically be released when your application exits, unlike any lock file, and its prior existence via a BindException is a pretty infallible sign that another instance is already running.

提交回复
热议问题