I want my application to check if another version of itself is already running.
For example, demo.jar started, user clicks to run it again, but the sec
Following solution work in two deadly scenerio too. 1> Even your launched exe scheduled as javaw.exe in task manager. 2> You can install your application at two location and from launching both location it also works.
String tempDir = System.getProperty("java.io.tmpdir");// dependent to OS find any tem dir.
String filePath = tempDir + "lockReserverd.txt";
try {
final File file = new File(filePath);
if(file.exists())
return false;
final RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw");
final FileLock fileLock = randomAccessFile.getChannel().tryLock();
if (fileLock != null) {
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
try {
fileLock.release();
randomAccessFile.close();
file.delete();
} catch (Exception e) {
//log.error("Unable to remove lock file: " + lockFile, e);
}
}
});
return true;
}
} catch (Exception e) {
//log.Error("Unable to create and/or lock file");
}
return false
or
This will work if your application.exe is listed in task manager
"tasklist /FI \"IMAGENAME eq "+MyApplication+".exe