0

I need to make a program, which can be executed in single instance. I tried to create a temporary file and delete it before exit program.

public static boolean isLocked() {
    File f = new File("lock.txt");
    return f.exists();
}

public static void lock() {
    String fname = "lock.txt";
    File f = new File(fname);
    try {
        f.createNewFile();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public static void unlock() {
    File f = new File("lock.txt");
    f.delete();
}

In frame

    private void initialize() {
    lock();
    }

    private void setFrameHandler() {
    frame.addWindowListener(new java.awt.event.WindowAdapter() {
        @Override
        public void windowClosing(java.awt.event.WindowEvent windowEvent) {
            unlock();
        }
    });
}

Problem occurs if program is finished with emergency (e.g. electricity cuts). File does not remove, and running a new instance is impossible. How to make a reliable single-instance verification?

6
  • 2
    What do you mean by the words "single instance"? Commented Jun 28, 2015 at 19:04
  • I meant that only one instance of a JAVA program can be executed at a certain time. If user launches jar file while program running, he gets error message. Commented Jun 28, 2015 at 19:07
  • 1
    Possible duplicate: stackoverflow.com/questions/7036108/…. Also related: rgagnon.com/javadetails/java-0288.html Simple solution: try creating ServerSocket. Commented Jun 28, 2015 at 19:07
  • 1
    Allowing a single instance... On the same machine? In the same datacenter? Globally? Commented Jun 28, 2015 at 19:08
  • @Andy Turner on the same machine Commented Jun 28, 2015 at 19:10

2 Answers 2

1

You could check for another instance of the program at startup using the GetProcesses method as described here But that only works depending on the scenario you have (might not see all processes of other users)

Another thing you could do is simply checking, if a specific file is locked via File.Open

File.Open ("path.lock", FileMode.OpenOrCreate, FileAccess.ReadWrite);

As long as you keep the resulting FileStream open in your program no other program can open the file in that mode either. This is basically how Unix lock files work too. Of course you have to catch an IOException (hinting you to a locked file).

Disclaimer: I did not try that code out so please check if I gave you the right parameters.

Edit: You could also check out this Code-Project article on how to do it with the win32 API

Another attempt using windows messaging has been done here

Sign up to request clarification or add additional context in comments.

Comments

0

A simple approach to this on a single machine is to write a 'PID file', which is literally a file containing the operating system's ID of the process currently running. You create this when you start your "critical" work, and remove it on successful completion.

Since it is unlikely that the process would be started again with the same PID, you can simply check to see if the PID file already exists, and if so, if that process is still running.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.