Java Open File Explorer

How to open file explorer in java

Sometimes, we come across requirements where we need to open a particular folder or directory in the native file explorer. In this tutorial, we will see how to do that, easy and effective.

Opening file explorer with java in any platform – Windows, Linux and Mac

The easiest way to do this is by using Java Desktop. The class provides an open() function Desktop#open(File) to open explorer when the given file is a directory.

private static void openDirectory() throws IOException {
  File directory = new File("C://Program Files//");
  Desktop.getDesktop().open(directory);
}

Open file explorer and select/highlight a file

Now, if you want to open the file explorer and have a file preselected, then there is a way for that as well! You can make use of Desktop.fileBrowseDirectory(File) to launch the file explorer and have a file highlighted. Let’s see a code example for the same.

However, before using this function, you have to keep the following things in mind.

  • The code will not work on ALL platforms.
  • You need to check whether the feature is supported via Desktop.getDesktop().isSupported(Action.BROWSE_FILE_DIR) function.
  • Minimum Java 9 is required
import java.awt.Desktop;
import java.awt.Desktop.Action;
import java.io.File;

public class JavaFileExplorer {

  public static void main(String[] args) {
    openDirectory();
  }

  private static void openDirectory() {
    //Check if the feature supported on your platform
    if (Desktop.getDesktop().isSupported(Action.BROWSE_FILE_DIR)) {
      File directory = new File("/home/afsal/Downloads/jdk-8u212-linux-x64.tar.gz");
      //Open directory with browse_file_dir option
      Desktop.getDesktop().browseFileDirectory(directory);
    }
  }
}

Open file explorer in Linux

Now, if you would like to add specific code for Linux/Ubuntu to open the file explorer, have a look into the following code snippet. We can make use of the xdg-open command to open any directory. The good thing about using this method is that, it works across multiple Linux distributions. They just have to have the xdg-open support.

private static void openDirectoryInLinux() throws Exception {
  Runtime.getRuntime().exec(
     new String[]{"sh", "-c", "/usr/bin/xdg-open '/home/genuinecoder/Downloads/'"}
   );
}

Open file explorer in Windows

Now, if you would like to add specific code for Windows OS to open the file explorer, have a look into the following code snippet. We will execute a cmd command to open Windows explorer.exe.

private static void openDirectoryInWindows() throws Exception {
  Runtime.getRuntime().exec("explorer C:\\");
}

Open file explorer in MacOS

If you are looking for specific code for macOS, you can make use of the /usr/bin/open function to open the directory on macOS.

private static void openDirectoryInMac() throws Exception {
   Runtime.getRuntime().exec(new String[]{"/usr/bin/open", file.getAbsolutePath()});
}

Conclusion

In this tutorial, we have learned how to open a directory from a java program. We have numerous methods to achieve the same goal here. We can use java.awt.Desktop for platform independent support and if platform-specific implementation is required, that is also available. You might also be interested in the following topics.

  1. Java TrayIcon Tutorial
  2. Directory / Folder Indexing Program Using Java

Muhammed Afsal Villan

Muhammed Afsal Villan is an experienced full-stack developer, specialized in desktop and mobile application development. He also regularly publishes quality tutorials on his YouTube channel named 'Genuine Coder'. He likes to contribute to open-source projects and is always enthusiastic about new technologies.

More From Author

JavaFX DatePicker Tutorial

JavaFx DatePicker Tutorial

JavaFX Custom Shaped Buttons

50 thoughts on “How to open file explorer in java

  1. Pingback: recurve bow
  2. Pingback: trustbet
  3. Pingback: sex trẻ em
  4. Pingback: b52club
  5. Pingback: ส่งsms
  6. Pingback: fuck boy 2024
  7. Pingback: Colt
  8. Pingback: putto33
  9. Pingback: dark168
  10. Pingback: Massage
  11. Pingback: ijoy bar vape pen
  12. Pingback: meetang168
  13. Pingback: slot99
  14. Pingback: ufa789

Leave a Reply