views:

1456

answers:

4

Hi,

I'm trying to figure out how to open system preferred editor for given file.

Say, we have a file manager, written in Java. User goes to folder and see list of files. And, for example, there are file "Icon.jpg". User double clicks on the filename and file opens in system's preferred editor (i.e. Gimp). The main issue is - how to do that?

We can do Runtime.getRuntime().exec("something file"), but this way you should know which program is preferred in user environment. But how?

We also are able to do Desktop.getDesktop().edit(File file), but this way we cannot track process and aren't able to know then this child process is closed. Other issue - function doesn't works on linux (at least on Ubuntu 8.10). There also is Desktop.getDesktop().open(File file), but it forses to open file viewer, instead of system viewer for that file type.

I am searching for solution all week, but didn't got any suitable and generic one. Do you know the other approaches to this question? For my project it would be enough if it would work on Windows+Linux+Mac.

Thank you for your answers and advices.

//Edit on 2009-02-08 23:04

Other suggestion: can I force "application selection" window in Windows and in Linux, as in Mac with "open file"? For example, then you trying to open file, you are being asked to choose application from list of system preferred ones? (something like "Open with..." in windows explorer). Do you know?

+1  A: 

This isn't cross-platform, but on Mac OS X you can do

Runtime.getRuntime().exec("open filename");

The open(1) executable uses LaunchServices to pick the right program to execute, and then uses that to open the file named filename.

Adam Rosenfield
Thanks, i will try that. It is very useful for me, because for now I am able to test my code only on Ubuntu and Linux and have no idea how is it on Mac. :)
Arturas
+5  A: 

Check out the java.awt.Desktop object. In your case, you want to invoke edit()

If you want to ensure that a given platform supports this call, then you can do something like the following (I have not tested this code):

public boolean editFile(final File file) {
  if (!Desktop.isDesktopSupported()) {
    return false;
  }

  Desktop desktop = Desktop.getDesktop();
  if (!desktop.isSupported(Desktop.Action.EDIT)) {
    return false;
  }

  try {
    desktop.edit(file);
  } catch (IOException e) {
    // Log an error
    return false;
  }

  return true;
}
Eddie
I definitely know, that edit() doesn't works on Ubuntu. But this functionality is required for me. Do you know what I supposed to do if edit() is not supported?
Arturas
+2  A: 

Seems that if you can't use java.awt.Desktop you have to distinguish between the OSes: Windows:

RUNDLL32.EXE SHELL32.DLL,OpenAs_RunDLL <file.ext>

Linux:

edit <file.ext>

Mac:

open <file.ext>

HTH. Obviously, that is not very portable...

Johannes Weiß
These are console commands, which I supposed to write to Runtime.getRuntime().exec() function, right?
Arturas
+3  A: 

Take a look at the Java Desktop Integration Components (JDIC).

This library allows you open or edit a file using the registered application with a simple static method call. You can also query what applications are registered for file types. It's a cross platform API, with support for desktop OSes.

erickson
Oh, great, I will definitely try to use this. Thanks.
Arturas