简体   繁体   English

在Java应用程序中使用文件关联

[英]Utilising a file association in a Java application

I am in the process of writing a cross platform Swing based application in which I want to utilize a file association which has been registered with the OS. 我正在编写一个基于Swing的跨平台应用程序,我希望利用已在操作系统中注册的文件关联。

So iv got to the point where I can click on a file and my app loads, but what I need to know is how I can get my application to know where the file is that launched it and then query the contents. 因此,我可以点击一个文件并加载我的应用程序,但我需要知道的是如何让我的应用程序知道启动它的文件的位置,然后查询内容。

Is there something further I have to do with the file association registration? 我还有什么与文件关联注册有关的东西吗? Or can Java do this for me? 或者Java可以为我做这个吗?

I'm not positive, but I'd expect that the name of the file you're processing by file click will end up in the arguments to your main() method. 我不是肯定的,但是我希望你通过文件点击处理的文件的名称最终会出现在你的main()方法的参数中。 Have you tried/checked that? 你试过/检查过吗?

If this is on Windows (you didn't specify): 如果这是在Windows上(您没有指定):

In the registry wherever you specified your application path for the file type registered to it, add to "%1" . 在注册表中,无论您为注册到的文件类型指定了应用程序路径,请添加到"%1" This is a special parameter Windows will fill in with the path of the file that was clicked. 这是Windows将使用单击的文件的路径填充的特殊参数。 So your registry entry would look something like c:\\path\\to\\app.exe "%1" 因此,您的注册表项看起来像c:\\path\\to\\app.exe "%1"

一种方法是让文件关联通过脚本或批处理文件运行Java应用程序,并让批处理文件将文件的路径名作为命令行参数,环境变量或Java属性传递。

Extensions can be linked to applications, you can setup the registry keys during installation. 扩展可以链接到应用程序,您可以在安装期间设置注册表项。 Which keys you need is documented here: 这里记录了您需要的密钥:

http://support.microsoft.com/?scid=kb%3Ben-us%3B185453&x=6&y=11 http://support.microsoft.com/?scid=kb%3Ben-us%3B185453&x=6&y=11

From java you can't access the windows registry in a direct way. 从java您无法直接访问Windows注册表。 Using Runtime you could do something like that http://www.rgagnon.com/javadetails/java-0480.html 使用Runtime,您可以执行类似http://www.rgagnon.com/javadetails/java-0480.html的操作

There're two commands on Windows that can help, assoc and ftype, so that you needn't do the dirty laundry to manipulate registry. Windows上有两个命令可以帮助,关联和ftype,这样你就不需要做脏衣服来操作注册表了。 Invoke the commands using, say, java.lang.Process. 使用java.lang.Process调用命令。 http://www.rgagnon.com/javadetails/java-0592.html http://www.rgagnon.com/javadetails/java-0592.html

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

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