48

From Java code, how do I open a specific folder (e.g. C:\Folder) in the platform's file explorer (e.g. Windows Explorer)? The examples are for Windows but I need a cross platform solution.

Nathan
  • 8,093
  • 8
  • 50
  • 76
Gett Oppus
  • 529
  • 1
  • 4
  • 3
  • 14
    I don't get why this is closed as not a real question. It seems very clear and useful to me. I do wish we could close some of the comments instead. – AnnTea Apr 23 '18 at 12:49

3 Answers3

90

Quite simply:

Desktop.getDesktop().open(new File("C:\\folder"));

Note: java.awt.Desktop was introduced in JDK 6.

Nathan
  • 8,093
  • 8
  • 50
  • 76
Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
19

Yes, you can do it with JDK 6 with the below code:

import java.awt.Desktop;
import java.io.File;
import java.io.IOException;

public class OpenFolder {
    public static void main(String[] args) throws IOException {
        Desktop desktop = Desktop.getDesktop();
        File dirToOpen = null;
        try {
            dirToOpen = new File("c:\\folder");
            desktop.open(dirToOpen);
        } catch (IllegalArgumentException iae) {
            System.out.println("File Not Found");
        }
    }
}

Note:

Desktop desktop = Desktop.getDesktop();

is not supported in JDK 5

Gokul Nath KP
  • 15,485
  • 24
  • 88
  • 126
12

Try

 Runtime.getRuntime().exec("explorer C:\bin");
Jaydeep Rajput
  • 3,605
  • 17
  • 35