Launch file from Java

Tor-Morten picture Tor-Morten · May 11, 2009 · Viewed 14.1k times · Source

I want to launch a file(a document) from a Java program and phase the following requirements:

  • Method must be applicabale on Mac, Win and Linux systems
  • I am not allowed to use "Runtime.getRuntime().exec("cmd.exe /C +"filename");
  • The file I am launching needs to be either of .doc / .docx / .rtf

The file is created runtime, a result from a report being created. Any good practices?

Answer

Joonas Pulakka picture Joonas Pulakka · May 11, 2009

Use Java Desktop API.

Desktop.getDesktop().open(new File(yourfilename));