From a983208dfb97d7618d229d6c6197a201454e4a70 Mon Sep 17 00:00:00 2001 From: Cian Ormond Date: Fri, 20 Jan 2023 18:24:28 -0800 Subject: [PATCH] Add lockfile to prevent duplicate instances Signed-off-by: Cian Ormond --- .../java/net/marvk/fs/vatsim/map/Application.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/main/java/net/marvk/fs/vatsim/map/Application.java b/src/main/java/net/marvk/fs/vatsim/map/Application.java index 6a0e3b76..e87f0e4d 100644 --- a/src/main/java/net/marvk/fs/vatsim/map/Application.java +++ b/src/main/java/net/marvk/fs/vatsim/map/Application.java @@ -5,9 +5,13 @@ import net.harawata.appdirs.AppDirsFactory; import net.marvk.fs.vatsim.map.data.VersionProvider; +import java.io.File; import java.io.IOException; +import java.nio.channels.FileChannel; +import java.nio.channels.FileLock; import java.nio.file.Files; import java.nio.file.Path; +import java.nio.file.StandardOpenOption; /** * TODO Hacky way to start a JavaFX application without messing with modules @@ -18,6 +22,16 @@ private Application() { } public static void main(final String[] args) throws IOException { + // Establish and check lockfile to prevent multiple instances + String userHome = System.getProperty("user.home"); + File file = new File(userHome, "vatprism.lock"); + FileChannel fc = FileChannel.open(file.toPath(), StandardOpenOption.CREATE, StandardOpenOption.WRITE); + FileLock lock = fc.tryLock(); + if (lock == null) { + System.out.println("Another instance of VATprism is running"); + System.exit(1); + } + reconfigureLogger(); SystemInformationLogger.logGeneralInformation(); App.main(args);