防止启动Java应用程序的多个实例 [英] Prevent launching multiple instances of a java application

查看:512
本文介绍了防止启动Java应用程序的多个实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想阻止用户多次并行运行我的java应用程序。

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, and delete the lock file when closing the application.

当应用程序运行时,您无法打开另一个jar实例。但是,如果通过任务管理器终止应用程序,则不会触发应用程序中的窗口关闭事件,也不会删除锁定文件。

When the application is running, you can not open an another instance of jar. However, if you kill the application through task manager, the window closing event in the application is not triggered and the lock file is not deleted.

如何确保锁文件方法有效或我可以使用其他机制吗?

How can I make sure the lock file method works or what other mechanism could I use?

推荐答案

类似的讨论是在
http://www.daniweb.com/software-development/java/threads/83331

绑定ServerSocket。如果它无法绑定,则中止启动。由于ServerSocket只能绑定一次,因此只能运行程序的单个实例。

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.

在你提出之前,没有。仅仅因为绑定了ServerSocket,并不意味着您对网络流量开放。只有在程序开始使用accept()监听端口时才会生效。

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().

这篇关于防止启动Java应用程序的多个实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆