2

i am having the following problem:

i wrote an application, in which the user can open some specific files. The user should be able to to select the editor, he wants to open the file with.

At the moment i am doing it with this piece of Code:

public void open(String path) {
    try {
        if(new File(path).exists())
            Runtime.getRuntime().exec("notepad.exe " + path);

    } catch (IOException e) {

    }
}

if i would change the Editor to ultraedit.exe for example, the Runtime wont be able to open it.

So now to my question, is there any way, to implement something like the open with function windows is using, and return all possible editors for a specific type of file? The extension of the file will be allways .ini

SomeJavaGuy
  • 7,307
  • 2
  • 21
  • 33
  • 2
    Is this of help? http://stackoverflow.com/questions/5197880/java-accessing-windows-open-with-list – Behe Jan 07 '13 at 09:30
  • wow thx, i did not find this question it really helped a lot. in the comment below, this piece of code does exectly what i want. `rundll32.exe shell32.dll,OpenAs_RunDLL C:\path\to\file.ext` – SomeJavaGuy Jan 07 '13 at 09:33

4 Answers4

1

As far as I can see the Runtime.exec() can't open ultraedit.exe because ultraedit.exe may not be found where your PATH Environment points to.

You need to detect where ultraedit.exe is located and then open it with a full-qualified path. This should work.

Stefan
  • 2,603
  • 2
  • 33
  • 62
  • ok, but how should i deal with the editors, i might not know, but the user wants to work with? – SomeJavaGuy Jan 07 '13 at 09:17
  • You can implement a dialog like Windows has to give the user the ability to tell where ultraedit.exe is placed. Than you can serialize this information and load it when you need those information. – Stefan Jan 07 '13 at 09:18
1

One approach (assumption: Not cross-platform, but Windows only) is to use the assoc and ftype commands from cmd.exe to list the applications associated with a specific file type, see also Utilising a file association in a Java application.

It is a two step process: first, get the file type (inifile) from the extension (.ini) through assoc, then lookup the executables associated to the file type through ftype:

C:> cmd.exe /c assoc .ini
.ini=inifile

C:> cmd.exe /c ftype inifile
inifile=%SystemRoot%\system32\NOTEPAD.EXE %1

You can launch these commands through Runtime.getRuntime().exec() and catch the output stream to get the associated program.

Community
  • 1
  • 1
Andreas Fester
  • 36,091
  • 7
  • 95
  • 123
  • Probably the link provided by @Behe is even more useful, one answer links to http://stackoverflow.com/questions/1238991/open-with-dialog-in-java - I am not sure if my approach allows to list all the associations which are shown in the "Open With ..." menu. It seems that it returns the default association only (used by the "Open" menu) – Andreas Fester Jan 07 '13 at 09:33
0

On XP at least (I have no way to check Win 7 at the moment) the Paths for installed applications can be found in the registry, under

\\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\<program name>

For an easier (and portable) solution I would add a configuration file listing editors and the full path to the executable.

Chris
  • 4,133
  • 30
  • 38
0

If available, you could try Desktop#open

Launches the associated application to open the file. If the specified file is a directory, the file manager of the current platform is launched to open it.

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366