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.

[547 byte] By [Anshuman_Taneja] at [2007-9-30 6:54:18]
# 1
this may help http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Runtime.html
terenaam at 2007-7-1 22:20:31 > top of Java-index,Administration Tools,Sun Connection...
# 2
> this may help> > http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Runtime.htmlWatch out for potential pitfalls when using Runtime.exec(): http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html
hungyee98 at 2007-7-1 22:20:31 > top of Java-index,Administration Tools,Sun Connection...
# 3
I had looked up that page before posting my question. Its not clear to me whether it contains what I need. Do you have any more specific information that can help me ?Any implementations out there ?-Anshuman
Anshuman_Taneja at 2007-7-1 22:20:31 > top of Java-index,Administration Tools,Sun Connection...
# 4

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()});

}

Anshuman_Taneja at 2007-7-1 22:20:31 > top of Java-index,Administration Tools,Sun Connection...
# 5
Now I'm curious.Can't I write a generic code that takes care of all common file types such as .PDF, .TXT ..
Rajeev.Asthana at 2007-7-1 22:20:31 > top of Java-index,Administration Tools,Sun Connection...
# 6
by generic you mean cross platform ?
Anshuman_Taneja at 2007-7-1 22:20:31 > top of Java-index,Administration Tools,Sun Connection...
# 7
The "start" command launches the registered application against its argument file.
BIJ001 at 2007-7-1 22:20:32 > top of Java-index,Administration Tools,Sun Connection...