Short Answer:
if (Desktop.isDesktopSupported()) {
Desktop.getDesktop().open(new File("C:\\"));
}
Long Answer:
Though reading what OS it is and then running an OS specific command would work, it entails, to an extent, hard coding of what needs to be done.
Let Java handle how each OS should open directories. Shouldn't be our headache to take. <3 abstraction.
Reading the #open(File) method documentation reveals that it will open the link on all OS' which support the operation. If the current platform doesn't support opening of folders or files (say a headless environment? of course, my guess as to why it won't open is conjecture), it will throw an UnsupportedOperationException
. If the user doesn't have access to read the folder (Windows Vista/7/8, Unix based machines), you will get a SecurityException
. So if you ask me, it's rather well handled.
Updated: added an if check before getting the Desktop
object so that your code is saved from nasty HeadlessException
and UnsupportedOperationException
exceptions as mentioned in the #getDesktop() Java Documentation.