简体   繁体   English

如何在SWT中使用默认应用程序打开文件?

[英]How to open file with default application in SWT?

I have for example .pdf file (path to that file). 我有例如.pdf文件(该文件的路径)。 How to open this file in default application (probably Acrobat Reader) from SWT application (for example on Button click) ? 如何在SWT应用程序的默认应用程序(可能是Acrobat Reader)中打开此文件(例如,单击按钮)?

You should be able to use: 你应该可以使用:

Program.launch(file);

to open the file (using the default application or creator). 打开文件(使用默认应用程序或创建者)。 From the javadoc: 来自javadoc:

Launches the operating system executable associated with the file or URL (http:// or https://). 启动与文件或URL关联的操作系统可执行文件(http://或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. 请注意,必须已存在Display才能保证此方法返回适当的结果。

Note that there are some peculiarities in Program.launch() (or at least there were, though these may have been fixed in more recent versions of the runtime.) I don't really remember the specifics of the bugs, but we do some checks to work around some issues: 请注意, Program.launch()中存在一些特性(或者至少有一些特性,尽管这些可能已在更新版本的运行时中得到修复。)我真的不记得错误的细节,但我们做了一些检查以解决一些问题:

  1. If you're on a Unix platform, and you're specifying an absolute path, there may be trouble opening that file. 如果您使用的是Unix平台,并且指定了绝对路径,则可能无法打开该文件。 We prefix absolute paths with /. 我们用/.前缀绝对路径/. - so that /tmp/foo would be translated to /./tmp/foo - although I don't really remember the specifics of this bug any more than that. - 这样/tmp/foo就会被翻译成/./tmp/foo虽然我真的不记得这个bug的具体细节了。

  2. On Windows, if you're trying to open a UNC path - for example \\\\server\\bar - you need to wrap the string in double-quotes. 在Windows上,如果您尝试打开UNC路径 - 例如\\\\server\\bar - 您需要将该字符串换成双引号。 For example: Program.open("\\"\\\\server\\bar\\""); 例如: Program.open("\\"\\\\server\\bar\\"");

Maybe this can help to find a decision: we ran into PermGen space trouble upon call Desktop.open() - which is in AWTpackage - out of our SWT application. 也许这有助于找到一个决定:我们在SWT应用程序中调用Desktop.open() (在AWTpackage中Desktop.open()时遇到了PermGen空间问题。

So I would prefer Program.launch() over Desktop.open() in a SWT-environment. 所以我更喜欢在SWT环境中的Desktop.open()使用Program.launch()

试试Desktop.open:

Desktop.getDesktop().open(file);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM