Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Ensuring a Single Program Instance on Desktop

 
Mark Mokris
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Mark Mokris
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic