Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - only allow once instance of application [duplicate]

Possible Duplicates:
How to implement a single instance Java application?
How do I make sure only one instance of my program can be executed?

What is a good and easy way to achieve this? I've read methods involving binding to a port. Will this interfere with other applications using the same method?

like image 489
Andy Hin Avatar asked Mar 21 '26 10:03

Andy Hin


2 Answers

Not good to bind to a port. Try starting the application and resetting the network adapter, see what happens. Other applications could also need to use that port.

I think it's better to implement a lock on an external resource, such as a file, but care must be taken: if the pc reboots or something like that, it will be necessary to detect the stale locks.

Have you checked the Apache commons or JBoss projects yet? i bet there must be a library to do this correctly.

like image 180
Mastermnd Avatar answered Mar 23 '26 00:03

Mastermnd


Only if they use the same port, and there are a lot of ports. Just don't use one of the common ones.

Some software will also use some sort of lock file. Thus a second instance won't start if it exists.

like image 43
Robin Avatar answered Mar 22 '26 22:03

Robin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!