Solved

restrict multiple instances in java

Posted on 2006-06-27
8
341 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SPLUNK REST  API call to Splunk to create and index? 2 99
triangle challenge 4 117
firstswap challenge 20 65
Please help| Getting the syntax error in below query. 2 42
I know it’s not a new topic to discuss and it has lots of online contents already available over the net. But Then I thought it would be useful to this site’s visitors and can have online repository on vim most commonly used commands. This post h…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

932 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now