Question
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?
Solution
Use Java Desktop API.
Desktop.getDesktop().open(new File(yourfilename));
OTHER TIPS
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.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow