Wednesday, July 19, 2006

Opening files in Java

I have spent a bit of time tonight looking into ways of opening files in Java such that they are loaded using their default viewer (i.e. loading MS Word documents in MS Word). There are the hacks that makes the OS-independent believer in me cringe, for example:
Runtime.getRuntime().exec("rundll32 SHELL32.DLL,ShellExec_RunDLL \""+fileName+"\"");
and
Runtime.getRuntime().exec(
"cmd /c \""+fileName+"\"");

Both will equivalently load any (?) file, but only within Windows...

Then I stumbled upon two similar projects:
The JDIC library and similar functionality in the soon-to-be released Java 6.
Both of these abstract away the underlying way in which to load the files.
This is mighty handy, and so I thought I'd post it here to let everyone know.

Cheers,
Jonathan Giles.

1 comment:

Jens said...

Thanks for posting this. I have used this kind of code years ago to pop up an external browser window. In general, Eclipse has a couple of APIs encapsulating additional platform dependent functionality (like the SWT browser class).