问题
I am trying to open *.docx files programmatically from Java on Mac OS X. For Windows and Linux I already got it working with the following methods:
Windows:
Runtime.getRuntime().exec(new String[] {"C:\Program Files\Microsoft Office\Office15\WINWORD.EXE", "document.docx"});
Linux:
Runtime.getRuntime().exec(new String[] {"/bin/sh", "-c", "/usr/bin/libreoffice", "document.docx"});
How does it work with Mac OS X ? My Microsoft Office installation is at the following location:
/Applications/Microsoft Office 2011/Microsoft Word.app
Any ideas highly appreciated - thanks.
回答1:
There is a program called open (/usr/bin/open), which accepts -a for an application, and also files passed, so you can do something like:
Runtime.getRuntime().exec(new String[] {"open", "-a", "Microsoft Word", "document.docx"});
回答2:
You can open it in all three operating systems using the Java Desktop API:
File myFile = new File("/path/to/mydoc.docx");
Desktop.getDesktop().open(myFile);
来源:https://stackoverflow.com/questions/25164067/how-to-open-file-in-microsoft-word-on-mac-os-x-from-within-java