How to open file in Microsoft Word on Mac OS X from within Java?

谁说胖子不能爱 提交于 2019-12-11 08:35:29

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!