sobota, 16. april 2011

Is my application running?

A neat way to check if a instance of an application is already running is to use FileChannel and it's ability to lock it.
An example:
File lockFile = new File("APP_NAME.lock");
FileChannel fileChannel = new RandomAccessFile(lockFile, "rw").getChannel();
FileLock fileLock = fileChannel.lock();


lock() will block, but you could use tryLock() to check it the fileChannel is already locked.
I've been using it in our application on Linux and Windows.