I have a Java project that I compile both into an exe with launch4j and a mac app with universalJavaApplicationStub. On Windows I can open a file with the exe (right-click→open with) and it will be passed as the first command line argument (in String[] args
).
On macOS I even registered DocumentTypes in the Info.plist. This activates the ability to drop files onto the app icon which was not possible before – so I at least know this part is set up correctly. However when I drop a file onto the app icon, do right-click→open with, or I run open myfile.txt -a myapp.app
the main function receives an empty list of arguments.
The open --help
command has the following line:
--args: All remaining arguments are passed in argv to the application's main() function instead of opened.
Using the parameter finally opens the file correctly. What does this description mean? How can I get the opened files instead of the arguments, so it works with right-click or dropping onto the app icon?
(I am using macOS Sonoma)
Using the comment from @Sweeper, I managed to figure out a solution using the
OpenFilesHandler
API. This is the minimal working example:MyApp.java
OpenMyAppFilesHandler.java