Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

One instance of application running

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,

I have an application and I want to ensure that only one instance of it is run, how do I do it? If an instance is already running, and the user executes another one, the application will prompt a message to the user, indicating another instance is already running...
 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What kinda Application is this?? Windows Application or Web Application??

Windows Application -- > Keep a text file for storing a variable say Application Status, Update this when the application is opened. say Started.
When the user shutsdown,update it back to Stopped. And access this file every time u start the application and also begin only if the status is Stopped.

Web Application ---> Have a cookie
 
Javeo Lineo
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Sri Ram ,

what kind of application is this?? its a standalone application / J2SE, it
runs on a UNIX OS...

the application wil ensure that only one instance of it will run, If an instance is already running, and the user executes another one, the application will prompt a message to the user, indicating another instance is already running...

thanks for the reply, greatly appreacited...
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instanciate a ServerSocket on a specific port. Only one application can do this - all others will get an IOException when trying. And the port is automatically freed when the application exits - even if the VM should crash or something...
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree, the server socket is the way to go. The file solution is asking
for trouble -- what if the program crashes and leaves the file behind
in the wrong state, or the user deliberately edits or deletes the file when
he shouldn't?
 
Javeo Lineo
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To Ilja Preuss, Jeff Albrechtsen and Sri Ram

Thanks for the reply, greatly appreacited...
 
Time is mother nature's way of keeping everything from happening at once. And this is a tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!