65

I think this will work only on an English language Windows installation:

System.getProperty("user.home") + "/Desktop";

How can I make this work for non English Windows?

jumar
  • 5,360
  • 8
  • 46
  • 42

9 Answers9

49

I use a french version of Windows and with it the instruction:

System.getProperty("user.home") + "/Desktop";

works fine for me.

Naman
  • 27,789
  • 26
  • 218
  • 353
Amlaa
  • 531
  • 4
  • 2
  • 9
    Do not do this. If the user has moved the desktop, this will not work! If you have a solid state C: drive it is quite common to move the desktop onto a different drive. This stops lots of writes to the SSD (reads don't reduce the lifetime, writes do) and it means you can have a small C: drive and a large normal one. – Steve Waring Jun 28 '15 at 22:36
  • On windows 7, at least, 'user.home' is set by a registry value at JVM startup. If you've moved your desktop in a 'normal' manor then using the above method should 'just work'. Also I'm pretty sure it actually looks for desktop when setting 'user.home' and then goes 1 directory up the path. – NekoKikoushi Jul 18 '19 at 18:41
9

I think this is the same question... but I'm not sure!:

In java under Windows, how do I find a redirected Desktop folder?

Reading it I would expect that solution to return the user.home, but apparently not, and the link in the answer comments back that up. Haven't tried it myself.

I guess by using JFileChooser the solution will require a non-headless JVM, but you are probably running one of them.

Community
  • 1
  • 1
Dan Gravell
  • 7,855
  • 7
  • 41
  • 64
  • 6
    The link mentioned above gives the correct answer. `File home = FileSystemView.getFileSystemView().getHomeDirectory();` and then if you need it as a string `home.getAbsolutePath();` – Steve Waring Jun 28 '15 at 22:53
9

This is for Windows only. Launch REG.EXE and capture its output :

import java.io.*;
    
public class WindowsUtils {
  private static final String REGQUERY_UTIL = "reg query ";
  private static final String REGSTR_TOKEN = "REG_SZ";
  private static final String DESKTOP_FOLDER_CMD = REGQUERY_UTIL 
     + "\"HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\" 
     + "Explorer\\Shell Folders\" /v DESKTOP";
   
  private WindowsUtils() {}
        
  public static String getCurrentUserDesktopPath() {
    try {
      Process process = Runtime.getRuntime().exec(DESKTOP_FOLDER_CMD);
      StreamReader reader = new StreamReader(process.getInputStream());

      reader.start();
      process.waitFor();
      reader.join();
      String result = reader.getResult();
      int p = result.indexOf(REGSTR_TOKEN);

      if (p == -1) return null;
      return result.substring(p + REGSTR_TOKEN.length()).trim();
    }
    catch (Exception e) {
      return null;
    }
  }

  /**
   * TEST
   */
  public static void main(String[] args) {
    System.out.println("Desktop directory : " 
       + getCurrentUserDesktopPath());
  }

  
  static class StreamReader extends Thread {
    private InputStream is;
    private StringWriter sw;

    StreamReader(InputStream is) {
      this.is = is;
      sw = new StringWriter();
    }

    public void run() {
      try {
        int c;
        while ((c = is.read()) != -1)
          sw.write(c);
        }
        catch (IOException e) { ; }
      }

    String getResult() {
      return sw.toString();
    }
  }
}

or you can use JNA

   Shell32.INSTANCE.SHGetFolderPath(null,
      ShlObj.CSIDL_DESKTOPDIRECTORY, null, ShlObj.SHGFP_TYPE_CURRENT,
      pszPath);
sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
RealHowTo
  • 34,977
  • 11
  • 70
  • 85
9
javax.swing.filechooser.FileSystemView.getFileSystemView().getHomeDirectory()
mauretto
  • 3,183
  • 3
  • 27
  • 28
3

Seems not that easy...

But you could try to find an anwser browsing the code of some open-source projects, e.g. on Koders. I guess all the solutions boil down to checking the HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\Desktop path in the Windows registry. And probably are Windows-specific.

If you need a more general solution I would try to find an open-source application you know is working properly on different platforms and puts some icons on the user's Desktop.

Grzegorz Oledzki
  • 23,614
  • 16
  • 68
  • 106
-2

You're just missing "C:\\Users\\":

String userDefPath = "C:\\Users\\" + System.getProperty("user.name") + "\\Desktop";
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
ABDO-AR
  • 160
  • 3
  • 9
  • 1
    This won't work [in some instances](https://stackoverflow.com/questions/1080634/how-to-get-the-desktop-path-in-java/68846720#comment50226011_10226697) where C is not the right drive. Hardcoded paths are generally not a good idea anyway. – Eric Aya Aug 19 '21 at 11:14
  • he need the desktop location and the desktop location is always in c drive – ABDO-AR Aug 19 '21 at 11:23
  • Did you know some better than using paths if you know please tell me i need that too but my way that i answer it's working fine with me. – ABDO-AR Aug 19 '21 at 11:24
  • 1
    Please not that this question is 12 years old and current Windows options might not have been available back then. Also, whilest not recommended, you can install Windows on another drive than C:. – Korashen Aug 19 '21 at 19:45
-4
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);
    }
}
Kemal Fadillah
  • 9,760
  • 3
  • 45
  • 63
  • 2
    This would only return the English version, which is not what the OP wants. – asteri Oct 29 '12 at 11:47
  • Do not do this. If the user has moved the desktop, this will not work! If you have a solid state C: drive it is quite common to move the desktop onto a different drive. This stops lots of writes to the SSD (reads don't reduce the lifetime, writes do) and it means you can have a small C: drive and a large normal one. – Steve Waring Jun 28 '15 at 22:36
-6

there are 2 things.

  1. you are using the wrong slash. for windows it's \ not /.
  2. i'm using RandomAccesFile and File to manage fles and folders, and it requires double slash ( \\ ) to separate the folders name.
Jean-François Corbett
  • 37,420
  • 30
  • 139
  • 188
-8

Simplest solution is to find out machine name, since this name is only variable changing in path to Desktop folder. So if you can find this, you have found path to Desktop. Following code should do the trick - it did for me :)

String machine_name = InetAddress.getLocalHost().getHostName();
String path_to_desktop = "C:/Documents and Settings/"+machine_name+"/Desktop/";
j-cup
  • 1
  • 1