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.
Quite simply:
Desktop.getDesktop().open(new File("C:\\folder"));
Note: java.awt.Desktop
was introduced in JDK 6.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With