tags:

views:

590

answers:

2

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?

+8  A: 

Use Java Desktop API.

Joonas Pulakka
+2  A: 

If you're running 1.6, use the Desktop API per mad-j's advice. If you're using an older VM (1.5 or earlier) you'll need to write your own platform-specific code to do this.

On the mac,

Runtime.getRuntime().exec(new String[] {"open", pathToFile});

On windows,

Runtime.getRuntime().exec(new String[] {"cmd.exe", "/C", pathToFile});

You may need to escape the path on windows.

Sam Barnum
He said he can't use cmd.exe
Valentin Rocher
There must be someone who's written some utility class for pre-1.6 JVMs.... ?
Jason S