Preventing multiple instances of an application: The file lock technique : FileLock « File Input Output « Java






Preventing multiple instances of an application: The file lock technique

 

import java.io.File;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;

class JustOneLock {
  private String appName;

  FileLock lock;

  FileChannel channel;

  public JustOneLock(String appName) {
    this.appName = appName;
  }

  public boolean isAppActive() throws Exception{
    File file = new File(System.getProperty("user.home"), appName + ".tmp");
    channel = new RandomAccessFile(file, "rw").getChannel();

    lock = channel.tryLock();
    if (lock == null) {
      lock.release();
      channel.close();
      return true;
    }
    Runtime.getRuntime().addShutdownHook(new Thread() {
      public void run() {
        try {
          lock.release();
          channel.close();
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    });
    return false;
  }
}

public class Main {
  public static void main(String[] args)throws Exception {
    JustOneLock ua = new JustOneLock("JustOneId");
    if (ua.isAppActive()) {
      System.out.println("Already active.");
      System.exit(1);
    } else {
      System.out.println("NOT already active.");
    }
  }
}

   
  








Related examples in the same category

1.Creating a Shared File Lock on a File
2.Creating a File Lock on a File