例如,我有 .pdf
文件(该文件的路径)。如何从 SWT 应用程序(例如单击按钮)在默认应用程序(可能是 Acrobat Reader)中打开此文件?
最佳答案
您应该能够使用:
Program.launch(file);
打开文件(使用默认应用程序或创建者)。来自 javadoc:
Launches the operating system executable associated with the file or URL (http:// or https://). If the file is an executable then the executable is launched. Note that a
Display
must already exist to guarantee that this method returns an appropriate result.
请注意 Program.launch()
中有一些特殊之处(或者至少有,尽管这些可能已在运行时的较新版本中得到修复。)我不太记得了bug 的具体细节,但我们会进行一些检查以解决一些问题:
如果您在 Unix 平台上,并且指定了绝对路径,则打开该文件可能会遇到问题。我们在绝对路径前加上
/.
- 这样/tmp/foo
就会被翻译成/./tmp/foo
- 虽然我不我真的不记得这个错误的细节了。在 Windows 上,如果您尝试打开 UNC 路径 - 例如
\\server\bar
- 您需要将字符串括在双引号中。例如:Program.open("\"\\server\bar\"");
关于java - 如何在 SWT 中使用默认应用程序打开文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9935092/