1

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?

Community
  • 1
  • 1
Feras Odeh
  • 9,136
  • 20
  • 77
  • 121
  • what do you mean? if you run a simple class or jar file that does not run extrenal files or create threads, then you are only running under one process? Or do you mean the JVM's process too? – David Kroukamp Jun 24 '12 at 17:58
  • A very simple approach with java NIO see complete example http://stackoverflow.com/a/20015771/185022 – AZ_ Nov 16 '13 at 07:06

5 Answers5

9

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"

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
2

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.

rooftop
  • 3,031
  • 1
  • 22
  • 33
  • 1
    No. Opening a ServerSocket is *much* simpler. No cleanup required, for a start. – user207421 Jun 24 '12 at 23:02
  • 1
    @EJP I have run into big trouble using a server socket. The application crashed and could not recover, the server socket was left in a zombie state, impossible to re-run without a reboot :( and that's why I went for the lock file (this was under Solaris) – GETah Jun 24 '12 at 23:26
  • @GETah Not possible unless someone connected to the server socket, which is not the case under discussion. – user207421 Jun 25 '12 at 00:29
1

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.

GETah
  • 20,922
  • 7
  • 61
  • 103
1

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.

dangowans
  • 2,263
  • 3
  • 25
  • 40
-5

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.

Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75