Solved

restrict multiple instances in java

Posted on 2006-06-27
8
344 Views
Last Modified: 2010-04-17
I have a java Application(Executable Jar File)

I want to restrict the multiple instances of that application(I.e If it is opened once we should not be able to open it again before it is closed)

Please help me regarding this
0
Comment
Question by:sree032397
  • 5
  • 2
8 Comments
 
LVL 25

Expert Comment

by:InteractiveMind
ID: 16991008
One technique is using a temporary file:


+---------------------+
| Application starts |
+----------+----------+
               |
+----------+----------+
| Check if file exists |--------<Yes>-------Program is already running
+----------+----------+
               |
            <No>
               |
+----------+----------+
|       Create file      |
+----------+----------+
               |
      Run program...
0
 
LVL 25

Expert Comment

by:InteractiveMind
ID: 16991013
Or you could do the same thing, but using a registry key instead of a file.

http://www.rgagnon.com/javadetails/java-0421.html
0
 
LVL 1

Author Comment

by:sree032397
ID: 16991772
Ok,
But Waht if Computer has got restarted Abruptly
??
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 25

Accepted Solution

by:
InteractiveMind earned 500 total points
ID: 16991802
Delete the file using a Shutdown hook.

For example:

Runtime.getRuntime().addShutdownHook( new Thread()
    {
        public void run()
        {
            // delete the file/registry key here
        }
    } ) ;


You can read up on it here:
http://www.onjava.com/pub/a/onjava/2003/03/26/shutdownhook.html
0
 
LVL 3

Expert Comment

by:chandru_in
ID: 16992143
Another trick would be creating a ServerSocket on a port.  However, this would waste a port on ur system.

Here is a sample code:-

import java.net.*;
import java.io.*;

public class Test {
      public static void main(String [] args) throws IOException {
            try {
                  ServerSocket sock = new ServerSocket(3345);
            } catch(BindException e) {
                  
                  System.exit(1);
            }

            // Remaining code goes here
      }
}
0
 
LVL 25

Expert Comment

by:InteractiveMind
ID: 16992167
This is also not recommended because it creates greater overhead, and also there's no guarantee that the socket will be closed when you want it to be. So, you may exit your program, but the socket doesn't close straight away; it could remain open for a long period of time still, which would prevent you from being able to open any new instances until the system finally cleans it up.


One alternative solution is using a service; but I think this is a bit of an overkill.
I personally stick to the file or registry key technique, which I suggested above.
0
 
LVL 3

Expert Comment

by:chandru_in
ID: 16992189
You can always close the socket using close() method.  If the system restarts abruptly the port would automatically be removed.
0
 
LVL 25

Expert Comment

by:InteractiveMind
ID: 16992203
No, even an invoke of close() does not _guarantee_ that the socket will actually be closed immediately; it's possible for a long lasting delay.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question