简体   繁体   English

设置启动器以打开关联的文件类型

[英]Setup launcher to open an associated file type

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)我希望我的 java 应用程序在用户选择具有该文件类型的任何内容时自动启动并显示 .txt 文件(Microsoft word 在您选择 .docx 文件时具有相同的行为)

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我在这里遵循了 jpackage 文档: 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.设置与我的 java 应用程序的文件关联。 I used this properties file to specify my application should open when a user selects a .txt file:我使用这个属性文件来指定当用户选择一个 .txt 文件时我的应用程序应该打开:

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.安装后,我的应用程序会在用户选择 .txt 文件时启动。

However, my application doesn't actually open the .txt file because I am unsure how to provide the file name/location to my application.但是,我的应用程序实际上并没有打开 .txt 文件,因为我不确定如何向我的应用程序提供文件名/位置。

I believe the solution is specifying another type of launcher with new args.我相信解决方案是使用新的 args 指定另一种类型的启动器。 For example:例如:

OpenTxtFile.properties:

arguments=openTextFile locationOfTextFile

but how can I actually pass the locationOfTextFile argument to the launcher?但是我怎样才能真正将 locationOfTextFile 参数传递给启动器呢?

To setup file associations your jpackage command needs to contain --file-associations FAtext.properties .要设置文件关联,您的jpackage命令需要包含--file-associations FAtext.properties Note that mime type is wrong above - should be text/plain请注意,上面的 mime 类型是错误的 - 应该是text/plain

After installing the new MSI file you can check the associations in new CMD.EXE:安装新的 MSI 文件后,您可以检查新 CMD.EXE 中的关联:

assoc |findstr .txt

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

See if the part after "=" is mentioned by ftype :查看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".请注意,Windows 可能已经为“txt”设置了默认应用程序,因此除非您访问“设置”>“应用程序”>“默认应用程序”,然后“按文件类型选择默认应用程序”,否则您的应用程序可能不会启动。

If your application gets launched after double click on txt file you should be passed the TXT file as first parameter.如果您的应用程序在双击 txt 文件后启动,您应该将 TXT 文件作为第一个参数传递。 JavaFX start() will print params with and you'll need to use : JavaFX start() 将打印参数,您需要使用:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM