Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open user system preferred editor for given file?

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

Say, we have a file manager, written in Java. User goes to folder and sees the list of files. And, for example, there is a 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 work on linux (at least on Ubuntu 8.10). There is also Desktop.getDesktop().open(File file), but it forces to open file viewer, instead of system viewer for that file type.

I am searching for a 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?

like image 758
Arturas Smorgun Avatar asked Feb 08 '09 17:02

Arturas Smorgun


2 Answers

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...

like image 63
Johannes Weiss Avatar answered Sep 27 '22 18:09

Johannes Weiss


This will work in windows

Runtime.getRuntime().exec( "CMD /C START filename.ext " );
like image 38
Shami Avatar answered Sep 27 '22 18:09

Shami