How to Find Out Default File Opener with Java?

前端 未结 2 1308
一整个雨季
一整个雨季 2021-01-05 19:42

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

相关标签:
2条回答
  • 2021-01-05 20:18

    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.

    0 讨论(0)
  • 2021-01-05 20:27

    Use this approach to call default opener and enjoy!

    public void playItems2(...) throws Exception {
        ...
        Process p = Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler c:/mp3/myfile.mp3");
        p.waitFor();
        ...
    }
    
    0 讨论(0)
提交回复
热议问题