Launching various external applications from a java application
I have several java.io.File objects displayed in my swing application. When the user double clicks any of these objects, I want to launch the external application associated with the file type.
For eg. if the user double clicks on "one.pdf", I want to launch Adobe Acrobat to view that file. I dont want to hard code application and file extension associations, rather I want to launch whatever it is that the underlying OS would launch given a double click on a certain file type.
_
Anshuman Taneja.
Let me answer my own question. Did not know the solution would be this direct. On the flip side, this is not a cross platform solution, and perhaps, there isnt any cross platform solution.
(in the code below, file is a java.io.File object.)
// if the file is an "exe", then it is launched like this
if(file.getName().indexOf(".exe") > 0 )
{
Runtime.getRuntime().exec("rundll32" + " " +
"url.dll,FileProtocolHandler" + " " + file.getName());
}
// for files which are not applications themselves,
// this will launch the application windows would've launched to open the file
else
{
Runtime.getRuntime().exec(new String[]
{"rundll32", "url.dll,FileProtocolHandler",
"file:///" + file.getAbsoluteFile()});
}