11 Replies Latest reply: Sep 17, 2007 1:59 PM by 807592 RSS

    Allow only one instance of a appln to run in a system

    807587
      Hi friends,
      Is it possible to allow a single instance of a java application to run in a system. When i tried to run another instance of the same application, it should say that already an instance is running.

      Is there anyway to do the above thing

      Rgds,
      Venkatesh P.
        • 1. Re: Allow only one instance of a appln to run in a system
          807587
          Cross-post:

          http://forum.java.sun.com/thread.jspa?threadID=609576
          • 2. Re: Allow only one instance of a appln to run in a system
            791266
            Search the forum. This question has been asked 112335821 times.

            /Kaj
            • 4. Re: Allow only one instance of a appln to run in a system
              JosAH
              Is it possible to allow a single instance of a java application to run in a system.
              When i tried to run another instance of the same application, it should
              say that already an instance is running. Is there anyway to do the above thing
              The 'unix way' of doing this is to make the application create a small file when
              it starts up; file creation is supposed to be an atomic operation at the OS and
              file system level.

              If the file already exists, another instance of the application is already running.
              Otherwise the file is created and the application starts up. When the application
              stops (after a successful startup), the lock file is deleted again.

              kind regards,

              Jos
              • 5. Re: Allow only one instance of a appln to run in a system
                807587
                But what if the application crashes before deleting the sentinel file?

                The Java way is to use a well-known port number and listen on the port. (ServerSocket).
                At most one process can listen on a given port at a time.
                • 6. Re: Allow only one instance of a appln to run in a system
                  807592
                  A simple way to ensure a single application instance in most Operating System is to create a lock file and open it (for writing) in your application. While your application is running that lock file cannot be deleted because the Operating System doesn't allow it.You can experiment this behavior opening a file for writing and trying to delete that file manually.
                  But only creating a lock file and opening it for writing is not enough. You should also do 2 things:
                  1) Check if the file exists. If the file doesn't exist, you know for sure that there is no other application running (use a proper name like 'your application name'.lock);
                  2) If the file exists, try to delete it. If the deletion of the lock file is not possible that means another application opened it for writing, and so, there is another instance of your application already running.
                  This solution also works if the application exists abnormally. The deletion of the lock file is not allowed while the application is running. If the application exits (abnormally or not) the deletion of the lock file is possible.
                  Although it's not mandatory, it is a good practice deleting the lock file after a normal exit of your application (you should close the object that handles the file writing before deleting it - otherwise it's not possible to delete it).That way, you know if the last use of the application was exited ok or not.
                  Here is the minimum code (without the deletion of the lock file when the application exits normally - I'm not deleting the lock file because it's not mandatory and many programmers thinks that it is. You can delete the lock file when the application exits normally to have additional information of the last use of your app).

                  try {
                  File file = new File("app.lock";); // use a proper lock file name.
                  if (file.exists()) { // if the file doesn't exist you know that there isn't another instance of your app.
                  System.out.println("Lock exists!");
                  if (file.delete()) { // test if the app can delete the lock file.
                  System.out.println("Lock deleted!"); // no other instance was locking the file
                  }
                  else {
                  System.out.println("An instance of this application is already running."); // the deletion wasn't possible because another instance of your application was locking the file.
                  System.exit(0);
                  }
                  }
                  } catch (Exception nop) {
                  }

                  try {
                  FileWriter lock = new FileWriter(app.lock";); // the same lock file name.
                  } catch (IOException nop) {
                  }

                  IMPORTANT: Do NOT close the lock file after opening it as a FileWriter object - that's the whole point (if you close the lock file, the OS allows the file deletion). If you want, you can close the FileWriter object just before the lock file deletion (if you've decided to delete the lock file after exiting).

                  Best regards,

                  Yassuo Toda.
                  • 7. Re: Allow only one instance of a appln to run in a system
                    807592
                    @Yassuo

                    This thread was 2yrs. ago the OP should know the answer by now.
                    before answering a post please look at the date.
                    • 8. Re: Allow only one instance of a appln to run in a system
                      807592
                      Sorry for that. There was an error on my previous message though. I corrected that bug in another topic (with a similar question) inside this forum. Best Regards, Yassuo.
                      • 9. Re: Allow only one instance of a appln to run in a system
                        DrClap
                        More to the point, your solution doesn't work on all operating systems. Plenty of them allow one process to delete a file that's being updated by a different process.
                        • 10. Re: Allow only one instance of a appln to run in a system
                          807592
                          Less to the point, in less than a week he has cut and paste that answer to something like 7 old threads asking the similiar question.
                          • 11. Re: Allow only one instance of a appln to run in a system
                            807592
                            Sorry for that, again. I'll pay more attention to the dates. Sorry...