问题
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?
回答1:
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?
回答2:
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"
回答3:
One way to do this is to have the file association run your Java app via a script or batch file, and have the batch file pass the pathname of the file as a command line argument, environment variable or Java property.
回答4:
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
回答5:
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
来源:https://stackoverflow.com/questions/2043029/utilising-a-file-association-in-a-java-application