File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Avoiding multiple instances Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Avoiding multiple instances" Watch "Avoiding multiple instances" New topic
Author

Avoiding multiple instances

Tom McAmmond
Ranch Hand

Joined: Feb 16, 2004
Posts: 58
Hi everyone....

What's the best way to disallow launching of multiple instances of my GUI application? Static flag, checked in main() method? Something else?

Thoughts? Questions? Comments?

Thanks!
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Javaoops,

Welcome to JavaRanch!

We ain't got many rules 'round these parts, but we do got one. Please change your display name to comply with The JavaRanch Naming Policy.

Thanks Pardner! Hope to see you 'round the Ranch!


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
satish sathineni
Ranch Hand

Joined: May 03, 2004
Posts: 46
I think the best practice would be to use "Singleton Pattern" design for your GUI class...

That is "Singleton Pattern" allows only one Instance for a particular class...

a.)
This is achieved by making the Constructor of the respective class as private method
There by no one can intialize the object in a routine way becoz the visibility of the
constructor is private..

b.)
To create the instance of that singleton class is done by providing a Static method which
returns the object of the class.
The logic for providing only single instance should go here in the Static method...

I can provide more info providing more details about the context and what is ur exact requirement.



Satish
Jez Nicholson
Ranch Hand

Joined: Oct 01, 2002
Posts: 39
Ah yes, synching on a physical file. Watch out for crashes as the file would still exist and you'd never be able to open the app again!
Javaoops
Ranch Hand

Joined: Jun 21, 2001
Posts: 57
Hi,

I hope, this link will fulfill your requirement.

http://coding.derkeiler.com/Archive/Java/comp.lang.java.gui/2004-09/0651.html
Tom McAmmond
Ranch Hand

Joined: Feb 16, 2004
Posts: 58
That's true.... It occurred to me that since my app runs in the taskbar using the JDIC taskbar code (jdic.dev.java.net) - ppl would likely just shut down their machine without properly quitting the program, and the lock file would stay in place. I must find a better way..... Thanks for the suggestions so far though! I'll post if I find another method
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

One thing you can do is open a ServerSocket for listening at a specific port that only your app uses. If the attempt fails, your app is apparently already running, so you exit. All the singleton-based solutions only work within a single JVM, you realize -- to actually prevent multiple copies, you need to use some kind of OS resource, be it a file, socket, or something else.


[Jess in Action][AskingGoodQuestions]
 
Don't get me started about those stupid light bulbs.
 
subject: Avoiding multiple instances