# [java] Getting a File handle to the Desktop

This topic is 4563 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm trying to get a JFileChooser to default to opening the Desktop of the user. Is there any way to retrieve a File object that points to the active user's desktop?

##### Share on other sites
What operating system?

Dave

##### Share on other sites
All of them. I thought Java was supposed to be platform independent.

But I'm really just worried about Windows.

##### Share on other sites
Well in 2000/XP it would be something like "<system driver letter here>:\Docuements and Settings\<logged on user here>\Desktop".

I believe in 9x it was just "C:\Windows\Desktop"

And just because something is Platform independant doesn't mean it still has to deal with the intracacies of the platform. What if I'm running in a command line only *nix environment. I can still use the Java VM, but there really isn't a desktop of any sort. So you can't really assume there is a desktop, and then that the desktops really follow any kind of convention.

##### Share on other sites
Quote:
 And just because something is Platform independant doesn't mean it still has to deal with the intracacies of the platform. What if I'm running in a command line only *nix environment. I can still use the Java VM, but there really isn't a desktop of any sort. So you can't really assume there is a desktop, and then that the desktops really follow any kind of convention.

By that same token, if I'm working on a *nix command line only environment and I instanciate a JFrame, it won't work. The Desktop is a large part of any consumer OS and it isn't unthinkable that such functionality would be useful.

But if there's no such class/package, I guess I'll have to do it the old fashioned way.

##### Share on other sites
The reason i asked was because of the path that needs to be generated, not cross platformability of Java.

Dave

##### Share on other sites
Quote:
 Original post by RattrapSo you can't really assume there is a desktop, and then that the desktops really follow any kind of convention.

So whatever imaginary functionality Java would have had for obtaining such a path should degrade gracefully in the case where it cannot satisfy the request. Omitting a useful piece of functionality just because there are three people in Inner Mongolia who wouldn't benefit from it is just silly.

(No offense to people from Inner Mongolia.)

##### Share on other sites
I would try the following:

new File(System.getProperty("user.home") + File.separator + "Desktop")

This should work well on MacOS X, Windows 2000/XP and most Unix or Linux deirvants (all?).

If this directory doesn't exist (do a check during installation / start-up), let the user select the Desktop directory with a FileDialog.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 10
• 11
• 15
• 11
• 11
• ### Forum Statistics

• Total Topics
634149
• Total Posts
3015832
×