Listen to the "open file with my java application" event in windows

The title is confusing, but I donโ€™t know how to explain it in a few words:

I have a Java application that reads * .example files. I also added file associations thanks to install4j , so my application starts when the user double-clicks on any file with the * .example extension

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

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

But I still do not understand how to use it and what to add to my application to listen to this event. Where can I find an example?

+6
source share
1 answer

Based on the documentation you are attached 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 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 also need to analyze the parameter string yourself.

+4
source

Source: https://habr.com/ru/post/943962/


All Articles