英文:
Setup launcher to open an associated file type
问题
我希望我的Java应用程序在用户选择任何具有该文件类型的内容时自动启动并显示一个.txt
文件(当用户选择一个.docx
文件时,与Microsoft Word具有相同的行为)。
我已经按照这里的jpackage文档进行了操作:https://docs.oracle.com/en/java/javase/14/jpackage/support-application-features.html#GUID-8D9F0607-91F4-4070-8823-02FCAB12238D
以设置我的Java应用程序的文件关联。我使用了这个属性文件来指定当用户选择一个.txt
文件时应该打开我的应用程序:
FAtext.properties:
mime-type=text/txt
extension=txt
description=Text source
这有效。安装后,用户选择一个.txt
文件时,我的应用程序会启动。
然而,我的应用程序实际上并不打开.txt
文件,因为我不确定如何将文件名/位置提供给我的应用程序。
我认为解决方案是使用新的参数指定另一种类型的启动器。例如:
OpenTxtFile.properties:
arguments=openTextFile locationOfTextFile
但我如何实际传递locationOfTextFile
参数给启动器呢?
英文:
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
得分: 2
要设置文件关联,您的jpackage
命令需要包含--file-associations FAtext.properties
。请注意,上面的 MIME 类型是错误的 - 应为text/plain
。
在安装新的 MSI 文件后,您可以在新的 CMD.EXE 中检查关联:
assoc | findstr .txt
.txt=progid.........
查看ftype
命令中等号后面的部分是否有提及:
ftype | findstr {等号后面的部分}
这应该会打印类似于以下内容:
progid.........="C:\Program Files\YOURAPP\YOURAPP.exe" %1
请注意,Windows 可能已经为“txt”设置了默认应用程序,因此您的应用程序可能不会启动,除非您访问“设置”>“应用”>“默认应用程序”,然后选择“按文件类型选择默认应用程序”。
如果在双击 txt 文件后启动您的应用程序,则应将 TXT 文件作为第一个参数传递。JavaFX 的 start()
函数将打印参数,您需要使用以下代码:
List<String> args = getParameters().getUnnamed();
System.out.println("start() args=" + args);
Path txt = Path.of(args.get(0));
英文:
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));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论