tags:

views:

893

answers:

5

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?

+1  A: 

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
+2  A: 

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

http://stackoverflow.com/questions/570401/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.

Dan Gravell
A: 

You cannot do this without asking Windows.

What is it you want to do with the users desktop?

Thorbjørn Ravn Andersen
A: 

This might be a little complicated to achieve but you can call SHGetSpecialFolderPath from the win32 api from java

Eric
A: 

This is for Windows only :

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();
    }
  }
}
RealHowTo