Search code examples
javainstall4jfile-association

listen for "open file with my java application" event on windows


Title is confusing, but don't know how to explain this in a few words:

I have a java application that reads *.example files. I've also added a file association thanks to install4j so my application is launched when the user double clicks any file with extension *.example

It seems that install4j sends the file path in the args[] so it should be easy to open that file and show it in my app. BUT what happens if the app is already running? I can only allow one instance of the application so, how can I know that the user is opening a file?

I've found this: http://resources.ej-technologies.com/install4j/help/api/com/install4j/api/launcher/StartupNotification.html

But I still don't understand how should I use it and what should I add in my app to listen for this event. Where can I find an example?


Solution

  • Based on the documentation you linked to, it looks like you can do this:

    StartupNotification.registerStartupListener(new StartupNotification.Listener() {
        public void startupPerformed(String parameters) {
            System.out.println("Startup performed with parameters " + parameters);
        }
    });
    

    Since startupPerformed will be called from different threads, you will need to make sure that the code that handles these notifications is thread-safe.

    The documentation also says:

    For multiple files, files are surrounded by double-quotes and separated by spaces.

    So you will need to parse the parameter string yourself as well.