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 usin开发者_开发问答g 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.
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();
...
}
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
- In the windows registry, look the default value for
HKEY_CLASSES_ROOT\.mp3
. Usually its"mp3file
". - 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.
精彩评论