Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

restrict multiple instances in java

Posted on 2006-06-27
8
Medium Priority
?
356 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 25

Accepted Solution

by:
InteractiveMind earned 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In this post we will learn different types of Android Layout and some basics of an Android App.
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Loops Section Overview

927 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