12

I know using .NET languages such as C#, one can do something like

Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)

to find the redirected location of the Desktop. However, under Java, I cannot think of a good way to do this. What is the most appropriate way to find a redirected user Desktop directory from Java, without using JNI? The specific purpose here is for the purposes of managing a desktop shortcut, if the user wants one, for a Java Web Start application.

This application needs to write to the "Application Data" tree as well as optionally to the Desktop. I am making the assumption that %APPDATA% is always correctly populated, even when folders are redirected, for finding the "Application Data" tree. So my open question is how to reliably find the Desktop folder.

NOTE: I believe that the Java system property ${user.home} actually (and erroneously) locates the user's Desktop directory via registry keys and then tries to navigate up one directory to find the "home" directory. This works fine when no directories are redirected, and otherwise may or may not return something useful.

Eddie
  • 53,828
  • 22
  • 125
  • 145

3 Answers3

32
FileSystemView filesys = FileSystemView.getFileSystemView();

filesys.getHomeDirectory()
Community
  • 1
  • 1
Russ Bradberry
  • 10,705
  • 17
  • 69
  • 85
  • 1
    How reliable is this? This method is totally undocumented even in Java 1.6. It looks like this is 100% Windows specific in behavior, which is OK, this leg of code would only be invoked on Windows anyway, but it's not reassuring. – Eddie Feb 20 '09 at 19:41
  • this method is NOT system specific and should work on any system please see the following documentation: http://littletutorials.com/2008/03/10/getting-file-system-details-in-java/ – Russ Bradberry Feb 20 '09 at 22:17
  • 1
    Thank you for the link. That reassures me. The lack of API documentation had me worried. Maybe for JDK7 I'll propose this JavaDoc be filled out, if it isn't already. – Eddie Feb 20 '09 at 23:26
  • 5
    This method does not work. `getHomeDirectory()` gives you the Desktop folder in Windows and home folder in Unix. – Oleg Mikheev Dec 02 '11 at 13:49
  • 3
    Is the second line (which gets roots) really related to the question? – Aurélien Bénel Feb 20 '14 at 11:04
-1
String desktopPath = System.getProperty("user.home") + File.separator + "Desktop";
-2
public class Sample {
   public static void main(String[] args) {    
      String desktopPath =System.getProperty("user.home") + "\\"+"Desktop";
      String s = "\"" + desktopPath.replace("\\","\\\\") + "\\\\" +"satis" + "\"";

      System.out.print(s);
      File f = new File(s);

      boolean mkdir = f.mkdir();
      System.out.println(mkdir);
   }
}
Vivek
  • 1,640
  • 1
  • 17
  • 34