Short answer:
if (Desktop.isDesktopSupported()) { Desktop.getDesktop().open(new File("C:\\")); }
Long answer: Although reading which OS it is, and then launching a specific operating system, will work, this is largely due to hard coding of what needs to be done.
Let Java handle how each OS should open directories. There should not be our headache. <3 abstractions.
Reading the #open (File) document shows that it will open a link to all operating systems that support this operation. If the current platform does not support opening folders or files (say, a headless environment, of course, my guess as to why it does not open is a hypothesis), it will throw an UnsupportedOperationException . If the user does not have read access to the folder (Windows Vista / 7/8, Unix-based machines), you will receive a SecurityException . Therefore, if you ask me, it is pretty well crafted.
Update: Added an if check before retrieving the Desktop object so that your code is saved from the disgusting HeadlessException and UnsupportedOperationException , as specified in the #getDesktop () Java Documentation .
source share