/InstanceLock

Prevents two instances of the same application running at the same time.

Primary LanguageJavaBSD 3-Clause "New" or "Revised" LicenseBSD-3-Clause

Instance Lock

Prevents two instances of the same application from starting.

Usage

Grab the JAR file either manually or via Maven.

<dependencies>
    <dependency>
        <artifactId>InstanceLock</artifactId>
        <groupId>com.github.mlk</groupId>
        <version>1.0</version>
    </dependency>
</dependencies>

Then during application start up create an InstanceLock¹ and check onlyInstance().

InstanceLock lock = new InstanceLock("application_name");
if (!lock.onlyInstance()) {
    // Already running.
    System.exist(-1);
}

If you want the second application to send data to the running application create an ApplicationStartupListener² and pass it into the constructor of the InstanceLock. applicationStartup(String) will be called when a second instance of the application is started. Note: You need to pass a string.

InstanceLock lock = new InstanceLock("application_name",
    new ApplicationStartupListener() {
        @Override
        public void applicationStartup(String message) {
            System.out.println(message);
        }
    });

if (!lock.onlyInstance("message")) {
    // Already running.
    System.exist(-1);
}