Java File Utility Class To Open Files Using JDIC (Java Desktop Integration Components)
In the Swing based java applications I write, the requirement usually exists to allow the app to open up other files such as Word Documents, PDF’s, HTML pages, etc. Since I try not to limit myself to just the file types defined in requirements gathering, I looked for a more general solution that will allow my apps to open any file type. After all, if on my Windows or Linux desktops I can double click on a file name to open the appropriate program, then why can’t my java applications?
My first take at this was to just use the java runtime, and assume my code would only run on windows 2000 or greater. The code at that point to open a file looked like this:
String cmd = "cmd /c \"" + fileName + "\"";
log.debug("Opening file with command: "+cmd);
java.lang.Runtime.getRuntime().exec(cmd);
Well, after that was in place for a few months, we wanted allow our application to also run on Linux. Uh oh, now the code above no longer works… continue reading…