Open a file with an external application on Java

Mercurious picture Mercurious · Dec 24, 2008 · Viewed 31.8k times · Source

How do you open a file from a java application when you do not know which application the file is associated with. Also, because I'm using Java, I'd prefer a platform independent solution.

Answer

RealHowTo picture RealHowTo · Dec 24, 2008

With JDK1.6, the java.awt.Desktop class can be useful.

public static void open(File document) throws IOException {
    Desktop dt = Desktop.getDesktop();
    dt.open(document);
}