Solved

restrict multiple instances in java

Posted on 2006-06-27
8
339 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Order table with macro 3 75
scoresClump  challenge 31 108
for loop with Set 4 45
wordmultiple challenge 12 90
This is about my first experience with programming Arduino.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
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 …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

707 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

15 Experts available now in Live!

Get 1:1 Help Now