How to implement a single instance Java application?

后端 未结 17 991
北荒
北荒 2020-11-22 04:32

Sometime I see many application such as msn, windows media player etc that are single instance applications (when user executes while application is running a new applicatio

17条回答
  •  迷失自我
    2020-11-22 04:57

    You can open a Memory Mapped File and then see if that file is OPEN already. if it is already open, you can return from main.

    Other ways is to use lock files(standard unix practice). One more way is to put something into the clipboard when main starts after checking if something is already in the clipboard.

    Else, you can open a socket in a listen mode(ServerSocket). First try to connect to hte socket ; if you cannot connect, then open a serversocket. if you connect, then you know that another instance is already running.

    So, pretty much any system resource can be used for knowing that an app is running.

    BR, ~A

提交回复
热议问题