从 Java 启动文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/847838/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Launch file from Java
提问by Tor-Morten
I want to launch a file(a document) from a Java program and phase the following requirements:
我想从 Java 程序启动一个文件(一个文档)并阶段性地满足以下要求:
- 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
- 方法必须适用于 Mac、Win 和 Linux 系统
- 我不允许使用 "Runtime.getRuntime().exec("cmd.exe /C +"filename");
- 我启动的文件需要是 .doc / .docx / .rtf
The file is created runtime, a result from a report being created. Any good practices?
该文件是运行时创建的,是创建报告的结果。有什么好的做法吗?
回答by Joonas Pulakka
回答by Sam Barnum
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.
如果您运行的是 1.6,请按照 mad-j 的建议使用桌面 API。如果您使用的是较旧的 VM(1.5 或更早版本),则需要编写自己的特定于平台的代码来执行此操作。
On the mac,
在 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.
您可能需要退出 Windows 上的路径。

