GeeCON Prague 2014*
The moose likes Java in General and the fly likes Ensuring a Single Program Instance on Desktop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "Ensuring a Single Program Instance on Desktop" Watch "Ensuring a Single Program Instance on Desktop" New topic
Author

Ensuring a Single Program Instance on Desktop

Mark Mokris
Ranch Hand

Joined: May 08, 2002
Posts: 61
I have a Java standalone application which supplies a GUI to the user (i.e. JFrame). It is a "standard" windows-type application running on the desktop. It is started using the java command.
I want no more than one instance of this program running on a user's desktop at a time. Right now, a user can run multiple versions of the program just by starting from additional command prompts. Is there a way to check to see if an instance of the program is already running on the desktop? I am running on an MS Windows platform.
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
There are many strategies for such a scenario, most of which involve some sort of external resource like a file or socket. Here is an example of what you could do using a socket:

[ May 30, 2003: Message edited by: Michael Morris ]

Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Mark Mokris
Ranch Hand

Joined: May 08, 2002
Posts: 61
Hey Mike. That is a cool idea. I will try it.
What other resources might I create? Maybe the ServerSocket will be perfect. I don't have to actually use the ServerSocket instance of course? Or do I?
The idea is that it acts like a "memory switch." Right?
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
What other resources might I create?
As I said you could use a file with a specific name and either delete that file are change its contents according to whether or not an app instance is up. The problem with that approach is what happens if the system crashes leaving the file in the up state? The best way would probably be to use JNDI the Java Naming and Directory Interface to bind your application to a name. If you would like to pursue that you can checkout JNDI and the JNDI tutorial.
I don't have to actually use the ServerSocket instance of course? Or do I?
Nope.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Ensuring a Single Program Instance on Desktop