Setup launcher to open an associated file type

293 views Asked by At

I would like my java application to automatically launch and display a .txt file when a user selects anything with that file type (The same behavior Microsoft word has when you select a .docx file)

I have followed the jpackage documentation here: https://docs.oracle.com/en/java/javase/14/jpackage/support-application-features.html#GUID-8D9F0607-91F4-4070-8823-02FCAB12238D

to setup file associations with my java application. I used this properties file to specify my application should open when a user selects a .txt file:

FAtext.properties:

mime-type=text/txt
extension=txt
description=Text source

This works. After installation my application starts when a user selects a .txt file.

However, my application doesn't actually open the .txt file because I am unsure how to provide the file name/location to my application.

I believe the solution is specifying another type of launcher with new args. For example:

OpenTxtFile.properties:

arguments=openTextFile locationOfTextFile

but how can I actually pass the locationOfTextFile argument to the launcher?

1

There are 1 answers

0
DuncG On BEST ANSWER

To setup file associations your jpackage command needs to contain --file-associations FAtext.properties. Note that mime type is wrong above - should be text/plain

After installing the new MSI file you can check the associations in new CMD.EXE:

assoc |findstr .txt

.txt=progid.........

See if the part after "=" is mentioned by ftype:

ftype | findstr {part after "=" above}

This should print something like:

progid.........="C:\Program Files\YOURAPP\YOURAPP.exe" %1

Note that Windows may already have Default Apps set up for "txt" so your app may not be launched unless you visit Settings > Apps > Default Apps then "Choose default applications by file type".

If your application gets launched after double click on txt file you should be passed the TXT file as first parameter. JavaFX start() will print params with and you'll need to use :

List<String> args = getParameters().getUnnamed();
System.out.println("start() args="+args);
Path txt = Path.of(args.get(0));