|
|
Re: Prompt user for an application to open a file with? [message #840499 is a reply to message #837661] |
Tue, 10 April 2012 06:49 |
Daniel Krügler Messages: 853 Registered: July 2009 |
Senior Member |
|
|
On 2012-04-06 02:17, Missing name Mising name wrote:
> I want to open arbitrary files from Eclipse. Currently I'm doing it like
> that:
>
> if (((File) selectedElement).isFile()) {
> try {
> Desktop.getDesktop().open((File) selectedElement);
> } catch (IOException e) {
> //TODO prompt for the appropriate application to open this file.
> e.printStackTrace();
> }
> }
>
> Unfortunately, this only works, if the OS has a default application
> associated with the file type. That's why, if there isn't any default
> application defined, I want to ask the user which application I should
> use. But I have no idea how to 1) find a list of available application
> and 2) open the file with that application. Any hints how to implement
> that in a platform-independent way?
As described by Hamed Mohammadi, Program.launch could be used. On
Windows, the built-in functionality will also ask the user for a program
selection if no associated application is found. Further, this function
returns false on failure, so you could still open your own dialog, if
all fails.
If this approach is not suitable for you, you could use something like
Program program = Program.findProgram("/your_file_extension/");
if (program != null) {
if (!program.execute(fileName)) {
/your error handling/
}
}
This works, if a program can be started with a provided file name (or
other string-like argument like an URL) successfully.
HTH & Greetings from Bremen,
Daniel Krügler
|
|
|
Powered by
FUDForum. Page generated in 0.03284 seconds