How to check if two instances of the same Java program are running?

Hi, I am making a program that will work through javaw.exe, so that it will not be visible directly. Now my problem once (accidentally) may happen that a user launches several instances of the same application,

for example enter javaw myProgram twice

In such a situation, is it possible to somehow prevent the launch of several instances of the same program if the program is already running.

+8
java
source share
2 answers

This article describes several ways to prevent this: http://www.rgagnon.com/javadetails/java-0288.html

+5
source share

Although file locking is probably the easiest and easiest option, I would like to mention one alternative: JIntellitype is a library that runs only on Windows and has a utility method to check if an application window with this title is running.

If it so happens that you are fine, having this function only on Windows, and using file locking does not work for you ... :)

+1
source share

Source: https://habr.com/ru/post/650325/


All Articles