Solved

restrict multiple instances in java

Posted on 2006-06-27
8
349 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Make the most of your online learning experience.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Progress

615 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