简体   繁体   中英

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.

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?

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. Have you tried/checked that?

If this is on Windows (you didn't specify):

In the registry wherever you specified your application path for the file type registered to it, add to "%1" . This is a special parameter Windows will fill in with the path of the file that was clicked. So your registry entry would look something like 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

From java you can't access the windows registry in a direct way. Using Runtime you could do something like that 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. Invoke the commands using, say, java.lang.Process. http://www.rgagnon.com/javadetails/java-0592.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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