views:

119

answers:

2

I need to find out default file opener for a given file on Windows so that I can customize the command arguments and open the file with the default opener/viewer.

My real usage scenario is opening multiple multimedia files with user's default media player so that all the files will be added to user's playlist (For the players that can open multiple files on the same intance). For operating system other than Windows I use Desktop.open(File file) method (I simply does not concern opening multiple files feature for OSs other than Windows), I cannot find any method which I can open multiple files other than customizing command arguments and running it using exec() method of the Runtime class. I use somethig similar to this:

private void playItems2(List<File> fileList, String playerBinary) {
    String args = " ";
    for (File file : fileList) {
        args += "\"" + file.getAbsolutePath() + "\" ";
    }

    try {
        String command = playerBinary + args;
        Runtime rt = Runtime.getRuntime();
        Process p = rt.exec(command);
    } catch (Exception exc) {/*handle exception*/
        System.err.println("Run Player Exc:" + exc.getMessage());
    }
}

I am using user specified path for the playerBinary, what I need to is automatically detecting default player for the first item of fileList and use it as playerBinary.

I have also looked at Rundll32.exe and cmd.exe /start solutions but they did not work for my usage scenario.

This question should not be confused with this and this.

+1  A: 

Cannot do with pure Java alone.

In case of Windows, you need to read registry. Suppose you need to find out file association for .mp3

  1. In the windows registry, look the default value for HKEY_CLASSES_ROOT\.mp3. Usually its "mp3file".
  2. Now look for HKEY_CLASSES_ROOT\mp3file\shell\open\command. The value in there is a string pointing to an executable to open .mp3 files with.

Now this cant be done in Java, you need to choose appropriate 3rd party lib to do this for you.

Suraj Chandran
A: 

Have you checked the JDesktop Integration Components at https://jdic.dev.java.net/ ?

It also includes a Java API to control native music players.

The JDesktop Integration Components (JDIC) project aims to make Java™ technology-based applications ("Java applications") first-class citizens of current desktop platforms without sacrificing platform independence. Its mission is to enable seamless desktop/Java integration.

JDIC provides Java applications with access to functionalities and facilities provided by the native desktop. It consists of a collection of Java packages and tools. JDIC supports a variety of features such as embedding the native browser, launching the desktop applications, creating tray icons on the desktop, registering file type associations, creating JNLP installer packages, etc. Many new features are contributed as incubator projects from the community.

mjustin