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?

mambetizm
  • 3
  • 1
  • 2
    What do you mean by the words "single instance"? – RealSkeptic Jun 28 '15 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. – mambetizm Jun 28 '15 at 19:07
  • 1
    Possible duplicate: http://stackoverflow.com/questions/7036108/prevent-launching-multiple-instances-of-a-java-application. Also related: http://www.rgagnon.com/javadetails/java-0288.html Simple solution: try creating ServerSocket. – Pshemo Jun 28 '15 at 19:07
  • 1
    Allowing a single instance... On the same machine? In the same datacenter? Globally? – Andy Turner Jun 28 '15 at 19:08
  • @Andy Turner on the same machine – mambetizm Jun 28 '15 at 19:10
  • Possible duplicate: https://stackoverflow.com/questions/177189/how-to-implement-a-single-instance-java-application – ZombieSpy Jun 28 '15 at 19:15

2 Answers2

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

rominator007
  • 1,553
  • 1
  • 11
  • 22
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.

Andy Turner
  • 137,514
  • 11
  • 162
  • 243