• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to make sure that only one instance is running?

 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a java class MyApplication.java, I am running it using "java MyApplication" in a command prompt on a Windows 2000 platform. Now I open another command prompt and try to run the same using the command "java MyApplication". I don't want the second instance to run. Is there any way to put a restriction of this kind?
Thanks,
Nagendra
SCJP1.4, SCWCD 1.3
 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi.
Since you're using two VMs, I reckon you are looking at some sort of an OS-level lock, i.e. - try to open a marker (empty) file, the first instance will get it, the second won't.
Nimo.
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lockfiles are a traditional way to achieve the effect, and a messy one (what if the application that set the lock terminates without removing the lockfile?).
A better way might be to open a socket on a set (and obscure) port.
Applications can check for the socket being open on the machine, if it is your application is running.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic