2

I am working on a GUI application that uses JavaFX(not fxml) and exported as a JAR. For slow machine, impatient user click more than once on JAR, and multiple instances of application started.

I'm looking for a solution to let only one instance can be run at a time on a system and if the user clicks again while the application is running nothing happens. I think it's called singleton but don't know how to implement it.

Ridham
  • 133
  • 6
  • does this help you? https://stackoverflow.com/questions/28642972/prevent-multiple-instance-of-jar-running – VFX Oct 31 '20 at 12:10
  • I heard about sockets but never used, i will let you know if it helps. – Ridham Oct 31 '20 at 12:19
  • *"For slow machine, impatient user click more than once on JAR,"*. If the application takes a long time to start up, consider showing a splash screen so that the user knows it has started. – James_D Oct 31 '20 at 16:32

4 Answers4

2

You could try JUnique. It's an open source library doing exactly what you ask for. Import junique-1.0.4.jar to your project as a library. It's just 10kb file.

It's manual neatly describes how to implement it on a project. For a JavaFX application, implementation would look something like this:

Make sure to import these classes to your main

import it.sauronsoftware.junique.AlreadyLockedException;
import it.sauronsoftware.junique.JUnique;
    public static void main(String[] args) {

        String appId = "myapplicationid";
        boolean alreadyRunning;
        try {
            JUnique.acquireLock(appId);
            alreadyRunning = false;
        } catch (AlreadyLockedException e) {
            alreadyRunning = true;
        }
        if (!alreadyRunning) {
            launch(args); // <-- This the your default JavaFX start sequence 
        }else{ //This else is optional. Just to free up memory if you're calling the program from a terminal.
            System.exit(1);
        }
    }
1

One easy solution that I've used is, when you start the application, it creates a file (I named it .lock but you can call it whatever you want), unless the file already exists, in which case the application terminates its execution instead of creating the file.

JustAnotherDeveloper
  • 2,061
  • 2
  • 10
  • 24
  • Currently JAR is in Documents folder but i don't see any files are created when application starts. Maybe i am looking at wrong place!! – Ridham Oct 31 '20 at 12:18
  • @Ridham Well, are you creating any files? Because that's what I meant: you create the file in your program if it doesn't exist, and if it exists you simply exit the program automatically. That way, even if users click the same jar many times it will only work once. – JustAnotherDeveloper Oct 31 '20 at 13:55
  • Now i got your point and it might work. But there might be a better solution. BTW Thanks. – Ridham Nov 01 '20 at 03:20
1

You will need to bind your application with a resource. It can be a file, port etc. You can change the code on startup to check if the file is locked. The below code will give you some idea

FileOutputStream foStream = new FileOutputStream("/tmp/testfile.txt");
FileChannel channel = fileOutputStream.getChannel();
FileLock lock = channel.lock();
sharad
  • 56
  • 4
0

If you'd properly package your JavaFX code as a real application instead of just throwing it into a jar, you might get that functionality for free and without all these hacks. If I package my JavaFX code on my Mac with the jpackage tool, the result will be a full featured macOS application. That means that when I double-click its icon somewhere several times, only one instance of the application will be started. This is the default behaviour on Macs and properly packaged JavaFX applications just stick to that rule too. I can't say however what the behaviour on Windows or Linux is because I currently don't have such a box running. Maybe someone who knows can add this as a comment.

mipa
  • 10,369
  • 2
  • 16
  • 35
  • Well, this app is not personal project and owner wants to built and export it using Ant. So properly packaging might help but it's not currently an option here. Thanks for for your help. – Ridham Nov 01 '20 at 04:02