How to implement 'locate in explorer/finder' function

zcaudate :

In a lot of programs, there is an option to locate a particular file using the native OS file explorer. How can this functionality be implemented using java?

cello :

Java provides java.awt.Desktop (API JDK 11), with which such interactions can be made:

File file = new File("/path/to/file.txt");
Desktop.getDesktop().open(file.getParentFile());

I use file.getParentFile() to open the directory containing the file, and not the file itself. If this line is executed, Finder (on macOs), Explorer (on Windows), or the default file browser on Linux will open a new window with the specified directory.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=104492&siteId=1