What is the best way to find the users home directory in Java?

后端 未结 9 578
清歌不尽
清歌不尽 2020-11-22 09:12

The difficulty is that it should be cross platform. Windows 2000, XP, Vista, OSX, Linux, other unix variants. I am looking for a snippet of code that can accomplish this for

相关标签:
9条回答
  • 2020-11-22 09:57

    The concept of a HOME directory seems to be a bit vague when it comes to Windows. If the environment variables (HOMEDRIVE/HOMEPATH/USERPROFILE) aren't enough, you may have to resort to using native functions via JNI or JNA. SHGetFolderPath allows you to retrieve special folders, like My Documents (CSIDL_PERSONAL) or Local Settings\Application Data (CSIDL_LOCAL_APPDATA).

    Sample JNA code:

    public class PrintAppDataDir {
    
        public static void main(String[] args) {
            if (com.sun.jna.Platform.isWindows()) {
                HWND hwndOwner = null;
                int nFolder = Shell32.CSIDL_LOCAL_APPDATA;
                HANDLE hToken = null;
                int dwFlags = Shell32.SHGFP_TYPE_CURRENT;
                char[] pszPath = new char[Shell32.MAX_PATH];
                int hResult = Shell32.INSTANCE.SHGetFolderPath(hwndOwner, nFolder,
                        hToken, dwFlags, pszPath);
                if (Shell32.S_OK == hResult) {
                    String path = new String(pszPath);
                    int len = path.indexOf('\0');
                    path = path.substring(0, len);
                    System.out.println(path);
                } else {
                    System.err.println("Error: " + hResult);
                }
            }
        }
    
        private static Map<String, Object> OPTIONS = new HashMap<String, Object>();
        static {
            OPTIONS.put(Library.OPTION_TYPE_MAPPER, W32APITypeMapper.UNICODE);
            OPTIONS.put(Library.OPTION_FUNCTION_MAPPER,
                    W32APIFunctionMapper.UNICODE);
        }
    
        static class HANDLE extends PointerType implements NativeMapped {
        }
    
        static class HWND extends HANDLE {
        }
    
        static interface Shell32 extends Library {
    
            public static final int MAX_PATH = 260;
            public static final int CSIDL_LOCAL_APPDATA = 0x001c;
            public static final int SHGFP_TYPE_CURRENT = 0;
            public static final int SHGFP_TYPE_DEFAULT = 1;
            public static final int S_OK = 0;
    
            static Shell32 INSTANCE = (Shell32) Native.loadLibrary("shell32",
                    Shell32.class, OPTIONS);
    
            /**
             * see http://msdn.microsoft.com/en-us/library/bb762181(VS.85).aspx
             * 
             * HRESULT SHGetFolderPath( HWND hwndOwner, int nFolder, HANDLE hToken,
             * DWORD dwFlags, LPTSTR pszPath);
             */
            public int SHGetFolderPath(HWND hwndOwner, int nFolder, HANDLE hToken,
                    int dwFlags, char[] pszPath);
    
        }
    
    }
    
    0 讨论(0)
  • 2020-11-22 09:57

    Others have answered the question before me but a useful program to print out all available properties is:

    for (Map.Entry<?,?> e : System.getProperties().entrySet()) {
        System.out.println(String.format("%s = %s", e.getKey(), e.getValue())); 
    }
    
    0 讨论(0)
  • 2020-11-22 10:01

    As I was searching for Scala version, all I could find was McDowell's JNA code above. I include my Scala port here, as there currently isn't anywhere more appropriate.

    import com.sun.jna.platform.win32._
    object jna {
        def getHome: java.io.File = {
            if (!com.sun.jna.Platform.isWindows()) {
                new java.io.File(System.getProperty("user.home"))
            }
            else {
                val pszPath: Array[Char] = new Array[Char](WinDef.MAX_PATH)
                new java.io.File(Shell32.INSTANCE.SHGetSpecialFolderPath(null, pszPath, ShlObj.CSIDL_MYDOCUMENTS, false) match {
                    case true => new String(pszPath.takeWhile(c => c != '\0'))
                    case _    => System.getProperty("user.home")
                })
            }
        }
    }
    

    As with the Java version, you will need to add Java Native Access, including both jar files, to your referenced libraries.

    It's nice to see that JNA now makes this much easier than when the original code was posted.

    0 讨论(0)
提交回复
热议问题