简体   繁体   中英

How to open file with default application in SWT?

I have for example .pdf file (path to that file). How to open this file in default application (probably Acrobat Reader) from SWT application (for example on Button click) ?

You should be able to use:

Program.launch(file);

to open the file (using the default application or creator). From the 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.

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:

  1. If you're on a Unix platform, and you're specifying an absolute path, there may be trouble opening that file. 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.

  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. For example: 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.

So I would prefer Program.launch() over Desktop.open() in a SWT-environment.

试试Desktop.open:

Desktop.getDesktop().open(file);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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