Search code examples
javawindowsexplorer

How to use java code to open Windows file explorer and highlight the specified file?


I am now using java Desktop API to manipulate file explorer. I know how to open the file explorer but I don't know how to open it and highlight the specified file.

As we using the Chrome, after downloading files, we can choose "show in folder" to open the file explorer and highlight the downloaded file.

How to use java Desktop API to do so? Or is there any other API in java can realize this action?


Solution

  • Use: Runtime.getRuntime().exec("explorer.exe /select,\"" + path + "\"");

    This also works if there is a space in the PATH.