File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes How to control the instances of my application Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to control the instances of my application" Watch "How to control the instances of my application" New topic

How to control the instances of my application

Ravi Gupta

Joined: May 16, 2001
Posts: 12
I have a Java application. I want to restrict the user to run the single instance of the application. How can i achieve this?
raimondas zemaitis
Ranch Hand

Joined: Feb 23, 2001
Posts: 104
a simple way to do this is to create temporary file in known location (user profile could be used for this) when application starts and delete it on exit. App should check if file exists prior to creation.
Nayanjyoti Talukdar
Ranch Hand

Joined: Feb 12, 2002
Posts: 71
I guess u wanna make a single instance of u'r application...For that matter, u can make use of singleton class.
karl koch
Ranch Hand

Joined: May 25, 2001
Posts: 388

the singelton pattern only prevents one instance of a class inside the same classloader/inside the same VM. If you start an application (trough: java com.some.MyApp) then you start an new VM and the classes are loaded with a new classloader.
the problem with the temp file is that after a crash of the app the temp file might stay on the disk and a restart of the app wont work because the file is present (perhaps the running app could rewrite the file every X seconds or so and if you start the app you check if the file is present AND NOT older that X seconds). Looks ugly to me but i dont have another idea....
good luck
I agree. Here's the link:
subject: How to control the instances of my application
It's not a secret anymore!