How to allow running only one instance of a Java program at a time?

前端 未结 9 2233
南旧
南旧 2020-11-30 00:07

I need to prevent users from starting my Java application (WebStart Swing app) multiple times. So if the application is already running it shouldn\'t be possible to start it

9条回答
  •  心在旅途
    2020-11-30 00:29

    Try JUnique:

    String appId = "com.example.win.run.main";
    boolean alreadyRunning;
    try {
        JUnique.acquireLock(appId);
        alreadyRunning = false;
    } catch (AlreadyLockedException e) {
        alreadyRunning = true;
    }
    if (alreadyRunning) {
        Sysout("An Instance of this app is already running");
        System.exit(1);
    }
    

提交回复
热议问题