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.
Asked
Active
Viewed 6.3k times
44
-
12I 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 Answers
84
Quite simply:
Desktop.getDesktop().open(new File("C:\\folder"));
Note: java.awt.Desktop was introduced in JDK 6.
Nathan
- 7,243
- 6
- 46
- 67
Buhake Sindi
- 85,564
- 27
- 164
- 223
-
1
-
This answer, as well as the other answers, do not work on my computer (Debian/KDE). Perhaps someone can come with solution that works on most platforms? (on my computer it should open the Dolphin file manager) – user42723 Feb 01 '18 at 00:17
-
18
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
- 14,745
- 24
- 85
- 122