Question

Possible Duplicate:
How to implement a single instance Java application?

is there a way to run only one instance of Java application so only I have one process? . is it possible to do it in java?

Was it helpful?

Solution

A simple way to have one instance is to use a service port.

ServerSocket ss = new ServerSocket(MY_PORT);

The benefit of using this approach instead of a locking a file is that you communicate to the instance already running and even check it is working. e.g. if you can't start the server socket use a plain Socket to send it a message like "open a file for me"

OTHER TIPS

The simplest way to do this would be to create a lock file on disk when the application starts if the file does not exist run normally. If the file does exist you can assume another instance of your application is running and exit with a message. Assuming I understood you question correctly.

If you mean "having one instance of your application running" then yes, you could use a lock file to acheive that. When your application starts, create a file and remove it when the program exits. At startup, check if the lock file is there. If the file exists then just exit as another instance of your application is already running.

You could open a socket on startup. If the socket is in use, there is probably already an instance of the app running. A lock file would work, but if your app crashes without deleting the lock file, you'll have to manually delete the file before you can start the app again.

You can apply Singleton Pattern

Following are the ways to do it:

1. Private Constructor and Synchronized method

public class MyClass{

   private static MyClass unique_instance;

   private MyClass(){

         // Initialize the state of the object here
    }

   public static synchronized MyClass getInstance(){

          if (unique_instance == null){

                 unique_instance = new MyClass();

           }

          return unique_instance;

      }

    }

2. Private Constructor and initializing the static instance during declaration

public class MyClass{

   private static MyClass unique_instance = new MyClass() ;

   private MyClass(){

         // Initialize the state of the object here
    }

   public static MyClass getInstance(){


          return unique_instance;

      }

    }

3. Double check Locking

public class MyClass{

   private static MyClass unique_instance;

   private MyClass(){

         // Initialize the state of the object here
    }

   public static MyClass getInstance(){

          if (unique_instance == null)

                      synchronized(this){

          if (unique_instance == null){

                 unique_instance = new MyClass();
               }
           }

          return unique_instance;

      }

    }

You can also implement a class with static methods and static variables to apply Singleton pattern, but its not recommended.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top