I’m currently using a temp folder for my Java application to create a lock so that only one instance can be running at a time. This lock file MUST be deleted when the program exits.
This is fine, except for the case when the process is killed (not allowed to shutdown normally, e.g. from Windows Task Manager). If that happens, the user will never be able to run the program again… eek
Anyone have a suggestion? Is there a way to make the OS delete a folder on reboot or something similar? Should I find another way to have a “single instance lock?”
Also, the solution should be cross-platform if possible…
I think the lock file mentioned is a good way to go:
– on application startup, check for the lock file
– if it doesn’t exist, create it and store the id of the process that
creates the file inside that file
– if it does exist, read the pid in the file and see if that process is
still running
– if not, replace the lock file and store your own process id inside
– if the process is still running, return an error and quit
Use this(javasysmon) to get the PID as well as the running processes on the box
You should add a ShutDownHook in your main to cleanup the folder, that will catch a large percentage of the terminations.
For more reliable hooking of the JVM look at this paper:
http://www.ibm.com/developerworks/java/library/i-signalhandling/