Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make sure that only a single instance of a Java application is running?

Tags:

java

mutex

I want my application to check if another version of itself is already running.

For example, demo.jar started, user clicks to run it again, but the second instance realizes "oh wait, there is already a demo.jar running." and quits with a message.

like image 798
Epicblood Avatar asked May 09 '12 01:05

Epicblood


People also ask

How will u make sure a single instance of an application in a system?

Making an application single instance, can be achieved by using a mutex (Mutual Exclusion Semaphore). A Windows application loads the main form through the Application. Run( ) method. In the Main method, create a new mutex.

How do I have one instance of a class?

In object-oriented programming, a singleton class is a class that can have only one object (an instance of the class) at a time. After the first time, if we try to instantiate the Singleton class, the new variable also points to the first instance created.

How do I know if Java application is running?

If you want to check the work of java application, run 'ps' command with '-ef' options, that will show you not only the command, time and PID of all the running processes, but also the full listing, which contains necessary information about the file that is being executed and program parameters.


2 Answers

What you are looking for can probably best be accomplished with a lock file. By lock file I simply mean a file that will have a predefined location and whose existence is your mutex.

Test if that file exists when your program starts, if it does, exit immediately. Create a file in a known location. If your program exits normally, delete the lock file.

Probably best is if you can also populate the file with a pid (process id) so that you can detect abnormal exits that didn't delete the file but this get OS specific.

like image 188
Andrew White Avatar answered Nov 16 '22 03:11

Andrew White


Enforce one instance of a program running with a ServerSocket Lock

Java Code. Put this into a file called Main.java:

import java.net.*;
import java.io.*;
public class Main{
  public static void main(String args[]){
    ServerSocket socket = null;
    try {
      socket = new ServerSocket(34567);
      System.out.println("Doing hard work for 100 seconds");
      try{ Thread.sleep(100000); } catch(Exception e){ }
      socket.close();
    }
    catch (IOException ex) {
      System.out.println("App already running, exiting...");
    }
    finally {
      if (socket != null)
          try{ socket.close(); } catch(Exception e){}
    }
  }
}

Compile and run it

javac Main.java
java Main

Test it in a normal case:

Run the program. You have 100 seconds to run the program again in another terminal, it will fall through saying its already running. Then wait 100 seconds, it should allow you to run it in the 2nd terminal.

Test it after force halting the program with a kill -9

  1. Start the program in terminal 1.
  2. kill -9 that process from another terminal within 100 seconds.
  3. Run the program again, it is allowed to run.

Conclusion:

The socket occupation is cleaned up by the operating system when your program is no longer operating. So you can be sure that the program will not run twice.

Drawbacks

If some sneaky person, or some naughty process were to bind all of the ports, or just your port, then your program will not run because it thinks its already running.

like image 41
Eric Leschinski Avatar answered Nov 16 '22 01:11

Eric Leschinski